40
Module 1 Introducing C# and the .NET Framework

10266A_01

Embed Size (px)

Citation preview

Page 1: 10266A_01

Module 1

Introducing C# and the .NET Framework

Page 2: 10266A_01

Module Overview

• Introduction to the .NET Framework 4

• Creating Projects Within Visual Studio 2010

• Writing a C# Application

• Building a Graphical Application

• Documenting an Application

• Debugging Applications By Using Visual Studio 2010

Page 3: 10266A_01

Lesson 1: Introduction to the .NET Framework 4

• What Is the .NET Framework 4?

• The Purpose of Visual C#

• What Is an Assembly?

• How the Common Language Runtime Loads, Compiles, and Runs Assemblies

• What Tools Does the .NET Framework Provide?

Page 4: 10266A_01

What Is the .NET Framework 4?

Common Language RuntimeCommon Language Runtime

Class Library Class Library

Development FrameworksDevelopment Frameworks

Page 5: 10266A_01

The Purpose of Visual C#

C# has been standardized and is described by the ECMA-334 C# Language Specification

C# uses a very similar syntax to C, C++, and Java

C# is the language of choice for many developers who build .NET Framework applications

C#

Page 6: 10266A_01

What Is an Assembly?

MyAssembly .dll OR .exe

Building blocks of .NET Framework applications

Collection of types and resources that form a logical unit of functionality

MyClassA

MyClassB

MyResource

V 1.1.254.1

Assembly version <major>.<minor>.<build>.<revision>

Assembly signed with a digital certificate

Page 7: 10266A_01

How the Common Language Runtime Loads, Compiles, and Runs Assemblies

Loads assemblies that the application referencesLoads assemblies that the application references

Verifies and compiles assemblies into machine codeVerifies and compiles assemblies into machine code

Runs the executable assemblyRuns the executable assembly

Assemblies contain MSIL code, which is not actually executable

The CLR loads the MSIL code from an assembly and converts it into the machine code that the computer requires

33

22

11

Page 8: 10266A_01

What Tools Does the .NET Framework Provide?

Makecert.exeCaspol.exe

Gacutil.exeNgen.exe

Ildasm.exe Sn.exe

Page 9: 10266A_01

Lesson 2: Creating Projects Within Visual Studio 2010

• Key Features of Visual Studio 2010

• Templates in Visual Studio 2010

• The Structure of Visual Studio Projects and Solutions

• Creating a .NET Framework Application

• Building and Running a .NET Framework Application

• Demonstration: Disassembling a .NET Framework Assembly

Page 10: 10266A_01

Key Features of Visual Studio 2010

Visual Studio 2010:Visual Studio 2010:

Intuitive IDE that enables developers to quickly build applications in their chosen programming languageIntuitive IDE that enables developers to quickly build applications in their chosen programming language

Visual Studio 2010 features:Visual Studio 2010 features:

Rapid application developmentServer and data accessDebugging featuresError handlingHelp and documentation

Rapid application developmentServer and data accessDebugging featuresError handlingHelp and documentation

Page 11: 10266A_01

Templates in Visual Studio 2010

Windows Forms ApplicationWindows Forms Application

Console ApplicationConsole Application

Class LibraryClass Library

ASP.NET Web ApplicationASP.NET Web Application

WCF Service ApplicationWCF Service Application

ASP.NET MVC 2 ApplicationASP.NET MVC 2 Application

Silverlight ApplicationSilverlight Application

WPF ApplicationWPF Application

Page 12: 10266A_01

The Structure of Visual Studio Projects and Solutions

Visual Studio SolutionVisual Studio Solution

Visual Studio solutions are wrappers for .NET projects

Visual Studio solutions can contain multiple .NET projects

Visual Studio solutions can contain different types of .NET projects

ASP.NET project

.aspx

.aspx.cs .config

.csproj

WPF project

.xaml

.xaml.cs .config

.csproj

Console project

.cs

.config

.csproj

Page 13: 10266A_01

Creating a .NET Framework Application

Open Visual Studio 2010Open Visual Studio 2010

On the File menu, click New, and then click ProjectOn the File menu, click New, and then click Project

In the New Project dialog box, specify the following, and then click OK:

- Project template

- Project name

- Project save path

In the New Project dialog box, specify the following, and then click OK:

- Project template

- Project name

- Project save path

Programmer productivity features include:

IntelliSenseIntelliSense

Code snippetsCode snippets

33

22

11

Page 14: 10266A_01

Building and Running a .NET Framework Application

Visual StudioVisual Studio

In Visual Studio 2010, on the Build menu, click Build SolutionIn Visual Studio 2010, on the Build menu, click Build Solution

On the Debug menu, click Start DebuggingOn the Debug menu, click Start Debugging

Command lineCommand line

csc.exe /t:exe /out:" C:\Users\Student\Documents\Visual Studio 2010\MyProject\myApplication.exe" "C:\Users\Student\Documents\Visual Studio 2010\MyProject\*.cs"

csc.exe /t:exe /out:" C:\Users\Student\Documents\Visual Studio 2010\MyProject\myApplication.exe" "C:\Users\Student\Documents\Visual Studio 2010\MyProject\*.cs"

11

22

Page 15: 10266A_01

Demonstration: Disassembling a .NET Framework Assembly

In this demonstration, you will:

• Run an existing .NET Framework application

• Open Ildasm

• Disassemble an existing .NET Framework assembly

• Examine the disassembled .NET Framework assembly

Page 16: 10266A_01

Lesson 3: Writing a C# Application

• What Are Classes and Namespaces?

• The Structure of a Console Application

• Performing Input and Output by Using a Console Application

• Best Practices for Commenting C# Applications

Page 17: 10266A_01

What Are Classes and Namespaces?

System.IO namespace System.IO namespace

A class is essentially a blueprint that defines the characteristics of an entityA class is essentially a blueprint that defines the characteristics of an entity

A namespace represents a logical collection of classes A namespace represents a logical collection of classes

File class Path class

DirectoryInfo class Directory class

FileInfo class

Page 18: 10266A_01

The Structure of a Console Application

using System; namespace MyFirstApplication{ class Program { static void Main(string[] args) {  } }}

using System; namespace MyFirstApplication{ class Program { static void Main(string[] args) {  } }}

Bring System namespace into scopeBring System namespace into scope

Program class declarationProgram class declaration

Main method declarationMain method declaration

Namespace declarationNamespace declaration

Page 19: 10266A_01

Performing Input and Output by Using a Console Application

ReadLine()ReadLine()

Clear()Clear()

ReadKey()ReadKey()

Write()Write()

WriteLine()WriteLine()

Read()Read()

System.Console method includes:

using System;...Console.WriteLine("Hello there!“); 

using System;...Console.WriteLine("Hello there!“); 

Page 20: 10266A_01

Best Practices for Commenting C# Applications

// This is a comment on a separate line.string message = "Hello there!"; // This is an inline comment.// This is a comment on a separate line.string message = "Hello there!"; // This is an inline comment.

Begin procedures by using a comment blockBegin procedures by using a comment block

In longer procedures, use comments to break up units of work 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 usedWhen 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 impliesWhen you write a decision structure, use a comment to indicate how the decision is made and what it implies

Page 21: 10266A_01

Lesson 4: Building a Graphical Application

• What Is WPF?

• The Structure of a WPF Application

• The WPF Control Library

• WPF Events

• Building a Simple WPF Application

• Demonstration: Building a Simple WPF Application

Page 22: 10266A_01

What Is WPF?

WPF is a new foundation for building Windows-based applications by combining:

WPF is a new foundation for building Windows-based applications by combining:

MediaMedia

DocumentsDocuments

Graphical user interfaceGraphical user interface

Features of WPF Features of WPF

Ease of user interface design

Extensive support for client

application development

Use of XAML

Support for interoperability with

older applications

Page 23: 10266A_01

The Structure of a WPF Application

<Window x:Class="WpfApplication1.MainWindow” xmlns=“..“ xmlns:x=“.." Title="MainWindow" Height="350" Width="525"> <Grid> </Grid></Window>

<Window x:Class="WpfApplication1.MainWindow” xmlns=“..“ xmlns:x=“.." Title="MainWindow" Height="350" Width="525"> <Grid> </Grid></Window>

Declarative XAML file

namespace WpfApplication1{ public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } }}

namespace WpfApplication1{ public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } }}

Code-behind Visual C# file

Page 24: 10266A_01

The WPF Control Library

ButtonButton

WPF controls include:

CanvasCanvas

ComboBoxComboBox

GridGrid

LabelLabel

StackPanelStackPanel

TextBoxTextBox

<Button Name="myButton" BorderBrush="Black" BorderThickness="1" Click="myButtonOnClick" ClickMode="Press"> Click Me</Button>

<Button Name="myButton" BorderBrush="Black" BorderThickness="1" Click="myButtonOnClick" ClickMode="Press"> Click Me</Button>

Button example:Button example:

Page 25: 10266A_01

WPF Events

<Button Name="myButton" Click="myButton_Click">ClickMe</Button><Button Name="myButton" Click="myButton_Click">ClickMe</Button>

Button definition

private void myButton_Click(object sender, RoutedEventArgs e){ // Code to do something goes here.}

private void myButton_Click(object sender, RoutedEventArgs e){ // Code to do something goes here.}

Event handler

Using WPF, you create event-driven applications, for example, responding to a button being clicked, item selections, and so on

Page 26: 10266A_01

Building a Simple WPF Application

Visual Studio enables you to: Visual Studio enables you to:

Create a new WPF applicationCreate a new WPF application

Add controls to the WPF applicationAdd controls to the WPF application

Set control propertiesSet control properties

Add event handlers to controlsAdd event handlers to controls

Add code to implement business logicAdd code to implement business logic

11

22

33

44

55

Page 27: 10266A_01

Demonstration: Building a Simple WPF Application

In this demonstration, you will:

• Create a new WPF application

• Add controls to the WPF application

• Set the properties for the controls

• Add code to the application

• Build and run the application

Page 28: 10266A_01

Lesson 5: Documenting an Application

• What Are XML Comments?

• Common XML Comment Tags

• Generating Documentation from XML Comments

Page 29: 10266A_01

What Are 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"); }}

/// <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"); }}

Use XML comments to generate Help documentation for your applicationsUse XML comments to generate Help documentation for your applications

Page 30: 10266A_01

Common XML Comment Tags

Common tags include: Common tags include:

<summary> … </summary><summary> … </summary>

<remarks> … </remarks><remarks> … </remarks>

<example> … </example><example> … </example>

<code> … </code><code> … </code>

<returns> … </returns><returns> … </returns>

Page 31: 10266A_01

Generating Documentation from XML Comments

Generate an XML file from Visual Studio 2010Generate an XML file from Visual Studio 2010

Generate an XML file from csc.exeGenerate an XML file from csc.exe

<?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>

<?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>

Consume the XML file in Sandcastle Help File BuilderConsume the XML file in Sandcastle Help File Builder

Page 32: 10266A_01

Lesson 6: Debugging Applications by Using Visual Studio 2010

• Debugging in Visual Studio 2010

• Using Breakpoints

• Stepping Through and Over Code

• Using the Debug Windows

Page 33: 10266A_01

Debugging in Visual Studio 2010

Debugging is an essential part of application developmentDebugging is an essential part of application development

Visual Studio 2010 provides several tools to help you debug codeVisual Studio 2010 provides several tools to help you debug code

Step Out

Step Over

Step Into Restart

Stop Debugging

Break AllStart Debugging

Page 34: 10266A_01

Using Breakpoints

When you run an application in Debug mode, you can pause execution and enter break modeWhen you run an application in Debug mode, you can pause execution and enter break mode

Visual Studio 2010 enables you to: Visual Studio 2010 enables you to:

Locate a specific line of code and set a breakpointLocate a specific line of code and set a breakpoint

Locate a breakpoint and disable itLocate a breakpoint and disable it

Locate a breakpoint and remove itLocate a breakpoint and remove it

Page 35: 10266A_01

Stepping Through and Over Code

You can step through code one statement at a time to see exactly how processing proceeds through your applicationYou can step through code one statement at a time to see exactly how processing proceeds through your application

Visual Studio 2010 enables you to: Visual Studio 2010 enables you to:

Step into the current statementStep into the current statement

Step over the current statementStep over the current statement

Step out of the current statementStep out of the current statement

Page 36: 10266A_01

Using the Debug Windows

Visual Studio 2010 includes several windows that you canuse to help debug your applicationsVisual Studio 2010 includes several windows that you canuse to help debug your applications

Locals

Output

Memory Processes

Modules

Call StackQuickWatch

Threads

Immediate

Page 37: 10266A_01

• Exercise 1: Building a Simple Console Application

• Exercise 2: Building a WPF Application

• Exercise 3: Verifying the Application

• Exercise 4: Generating Documentation for an Application

Logon information

Estimated time: 60 minutes

Lab: Introducing C# and the .NET Framework 

Page 38: 10266A_01

Lab Scenario

Page 39: 10266A_01

Lab Review

Review Questions

• What methods did you use to capture and display information in your console application?

• What event did you handle on the Format Data button in your WPF application?

• What debugging functions did you use when you verified the application?

• How do you instruct Visual Studio 2010 to produce an XML file that contains XML comments?

Page 40: 10266A_01

Module Review and Takeaways

• Review Questions

• Best Practices

• Tools