Avanade Capability - NET - CSharp - Module 2 - Overview of CSharp

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

Module 2:

Overview of C#

First Name Last Name 20pt


Job Title 20pt

Today’s Date 18pt

© Copyright 2011 Avanade Inc. All Rights Reserved. 1


The Avanade name and logo are registered trademarks in the US and other countries.
Module Overview

• Structure of a C# Program
• Basic Input/Output Operations
• Documenting an Application
• Debugging Applications by Using Visual Studio 2010

© Copyright 2011 Avanade Inc. All Rights Reserved. 2


Lesson 1:
Structure of a C# Program

© Copyright 2011 Avanade Inc. All Rights Reserved. 3


Structure of a C# Program

• Hello, World
• The Class
• The Main Method
• The using Directive and the System Namespace
• Demonstration: Using Visual Studio to Create a C#
Program

© Copyright 2011 Avanade Inc. All Rights Reserved. 4


The Structure of a Console Application

[2.1.1 Structure]
Bring System namespace into scope

using System;
Namespace declaration
namespace MyFirstApplication
{ Program class declaration
class Hello
{
static void Main( ) Main method declaration
{
Console.WriteLine(“Hello, World”);
}
}
}

© Copyright 2011 Avanade Inc. All Rights Reserved. 5


The Class

• A C# application is a collection of classes, structures, and


types
• A class is a set of data and methods
• Syntax
class name
{
...
}
• A C# application can consist of many files
• A class can span multiple files

© Copyright 2011 Avanade Inc. All Rights Reserved. 6


The Main Method

• When writing Main, you should:


– Use an uppercase “M”, as in “Main”
– Designate one Main as the entry point to the program
– Declare Main as public static void Main
• Multiple classes can have a Main
• When Main finishes, or returns, the application quits

© Copyright 2011 Avanade Inc. All Rights Reserved. 7


The using Directive and the System
Namespace
• The .NET Framework provides many utility classes
– Organized into namespaces
• System is the most commonly used namespace
• Refer to classes by their namespace
System.Console.WriteLine("Hello, World");

• The using directive


using System;

Console.WriteLine("Hello, World");

© Copyright 2011 Avanade Inc. All Rights Reserved. 8


Lesson 2:
Basic Input / Output Operations

© Copyright 2011 Avanade Inc. All Rights Reserved. 9


Basic Input / Output Operations

• The Console Class


• Write and WriteLine Methods
• Read and ReadLine Methods

© Copyright 2011 Avanade Inc. All Rights Reserved. 10


The Console Class

• Provides access to the standard input, standard output,


and standard error streams
• Only meaningful for console applications
– Standard input – keyboard
– Standard output – screen
– Standard error – screen
• All streams may be redirected

© Copyright 2011 Avanade Inc. All Rights Reserved. 11


Performing Input and Output by Using a Console
Application
System.Console method includes:

Write()

WriteLine()

Read()

ReadKey()

ReadLine()

Clear()

[2.2.1 Write / 2.2.2 WriteLine / 2.2.3 Read / 2.2.4 ReadKey / 2.2.5 ReadLine / 2.2.6 Clear]

using System;
...
Console.WriteLine("Hello there!“);
© Copyright 2011 Avanade Inc. All Rights Reserved. 12
Text Formatting

• The format string specifies how the data is output and it


can contain makers which are replaced in order by the
parameters that follow. [2.2.7 Text Formatting / 2.2.8 Text Formatting]

Console.WriteLine(“The sum of {0} and {1} is {2}”, 100, 130,


100+130);

© Copyright 2011 Avanade Inc. All Rights Reserved. 13


Numeric Formatting

• The full syntax for the format string is {N,M:FormatString},


where N is the parameter number, M is the field width and
justification, and FormatString specifies how numeric data
should be displayed. [2.2.9 Numeric Formatting]

Item Meaning

C Display the number as currency, using the local currency


symbol and conventions.

D Display the number as a decimal integer.

E Display the number by using exponential (scientific) notation.

© Copyright 2011 Avanade Inc. All Rights Reserved. 14


Numeric Formatting (continued)

Item Meaning

F Display the number as a fixed-point value.

G Display the number as either fixed point or integer, depending


on which format is the most compact.

N Display the number with embedded commas.

X Display the number by using hexadecimal notation.

© Copyright 2011 Avanade Inc. All Rights Reserved. 15


Lesson 3:
Documenting an Application

© Copyright 2011 Avanade Inc. All Rights Reserved. 16


Documenting an Application

• What Are XML Comments?


• Common XML Comment Tags
• Generating documentation from XML Comments

© Copyright 2011 Avanade Inc. All Rights Reserved. 17


Commenting Applications

• Comments are important


– A well-commented application permits a developer to fully
understand the structure of the application
• Single-line comments [2.3.1 Single Comment]

// Get the user’s name


Console.WriteLine("What is your name? ");
name = Console.ReadLine( );

• Multiple-line comments [2.3.2 Multiple Comment]

/* Find the higher root of the


quadratic equation */
x = (…);

© Copyright 2011 Avanade Inc. All Rights Reserved. 18


Best Practices for Commenting C# Applications

Begin procedures by using a comment block

In longer procedures, use comments to break up


units of work

When you declare variables, use a comment to indicate


how the variable will be used

When you write a decision structure, use a comment to


indicate how the decision is made and what it implies

// This is a comment on a separate line.


string message = "Hello there!"; // This is an inline comment.

© Copyright 2011 Avanade Inc. All Rights Reserved. 19


What Are XML Comments?

Use XML comments to generate Help documentation for


your applications

[2.3.3 XML Comments]

/// <summary> The Hello class prints a greeting on the screen


/// </summary>
public class Hello
{
/// <summary> We use console-based I/O. For more information
/// about
/// WriteLine, see <seealso cref="System.Console.WriteLine"/>
/// </summary>
public static void Main( )
{
Console.WriteLine("Hello World");
}
}

© Copyright 2011 Avanade Inc. All Rights Reserved. 20


Common XML Comment Tags
[2.3.4 XML Comment Tags]

Common tags include:

<summary> … </summary>

<remarks> … </remarks>

<example> … </example>

<code> … </code>

<returns> … </returns>

© Copyright 2011 Avanade Inc. All Rights Reserved. 21


Common XML Comment Tags (cont.)

/// <summary> The Hello class prints a greeting


/// on the screen
/// </summary>
class Hello
{
/// <remarks> We use console-based I/O.
/// For more information about WriteLine, see
/// <seealso cref="System.Console.WriteLine"/>
/// </remarks>
public static void Main( )
{
Console.WriteLine("Hello, World");
}
}
© Copyright 2011 Avanade Inc. All Rights Reserved. 22
Generating Documentation from XML Comments
Generate an XML file from Visual Studio 2010

Generate an XML file from csc.exe

[2.3.5 Generating Documentation]

<?xml version="1.0"?>
<doc>
<assembly> <name>MyProject</name></assembly>
<members>
<member name="T:Hello">
<summary> The Hello class prints a greeting on the screen
</summary>
</member>
<member name="M:Hello.Main">
...
</member>
</members>
</doc>

© Copyright 2011 Avanade Inc. All Rights Reserved. 23


Generating XML Documentation

• You can compile the XML tags and documentation into an


XML file by using the C# compiler with the /doc option:
csc myprogram.cs /doc:mycomments.xml

• If there are no errors, you can view the XML file that is
generated by using a tool such as Internet Explorer.

© Copyright 2011 Avanade Inc. All Rights Reserved. 24


Exception Handling
[2.3.6 Exception Handling]

using System;
public class Hello
{
public static void Main(string[ ] args)
{
try{
Console.WriteLine(args[0]);
}
catch (Exception e) {
Console.WriteLine("Exception at
{0}", e.StackTrace);
}
}
}

© Copyright 2011 Avanade Inc. All Rights Reserved. 25


Module Review and Takeaways

• Review Questions
• Best Practices

© Copyright 2011 Avanade Inc. All Rights Reserved. 32

You might also like