10266a_01

Upload: miguelewrs

Post on 16-Oct-2015

53 views

Category:

Documents


0 download

TRANSCRIPT

  • Module 1Introducing C# and the .NET Framework

  • Module OverviewIntroduction to the .NET Framework 4Creating Projects Within Visual Studio 2010Writing a C# ApplicationBuilding a Graphical ApplicationDocumenting an ApplicationDebugging Applications By Using Visual Studio 2010

  • Lesson 1: Introduction to the .NET Framework 4What Is the .NET Framework 4?The Purpose of Visual C#What Is an Assembly?How the Common Language Runtime Loads, Compiles, and Runs AssembliesWhat Tools Does the .NET Framework Provide?

  • What Is the .NET Framework 4?Common Language RuntimeClass Library Development Frameworks

  • What Is the .NET Framework 4?

  • The Purpose of Visual C#C# has been standardized and is described by the ECMA-334 C# Language SpecificationC# uses a very similar syntax to C, C++, and JavaC# is the language of choice for many developers who build .NET Framework applicationsC#

  • The Purpose of Visual C#C#

  • What Is an Assembly?MyAssembly .dll OR .exeBuilding blocks of .NET Framework applicationsCollection of types and resources that form a logical unit of functionalityMyClassAMyClassBMyResourceV 1.1.254.1Assembly version ...Assembly signed with a digital certificate

  • What Is an Assembly?

  • How the Common Language Runtime Loads, Compiles, and Runs AssembliesLoads assemblies that the application referencesVerifies and compiles assemblies into machine codeRuns the executable assemblyAssemblies contain MSIL code, which is not actually executableThe CLR loads the MSIL code from an assembly and converts it into the machine code that the computer requires321

  • What Is an Assembly?

  • What Tools Does the .NET Framework Provide?Makecert.exeCaspol.exeGacutil.exeNgen.exeIldasm.exeSn.exe

  • Lesson 2: Creating Projects Within Visual Studio 2010 Key Features of Visual Studio 2010Templates in Visual Studio 2010The Structure of Visual Studio Projects and SolutionsCreating a .NET Framework ApplicationBuilding and Running a .NET Framework ApplicationDemonstration: Disassembling a .NET Framework Assembly

  • Key Features of Visual Studio 2010Visual Studio 2010:Intuitive IDE that enables developers to quickly build applications in their chosen programming languageVisual Studio 2010 features:Rapid application developmentServer and data accessDebugging featuresError handlingHelp and documentation

  • Templates in Visual Studio 2010Windows Forms ApplicationConsole ApplicationClass LibraryASP.NET Web ApplicationWCF Service ApplicationASP.NET MVC 2 ApplicationSilverlight ApplicationWPF Application

  • The Structure of Visual Studio Projects and SolutionsVisual Studio SolutionVisual Studio solutions are wrappers for .NET projectsVisual Studio solutions can contain multiple .NET projectsVisual Studio solutions can contain different types of .NET projectsASP.NET project.aspx.aspx.cs.config.csprojWPF project.xaml.xaml.cs.config.csprojConsole project.cs.config.csproj

  • Creating a .NET Framework ApplicationOpen Visual Studio 2010On the File menu, click New, and then click ProjectIn the New Project dialog box, specify the following, and then click OK: - Project template - Project name - Project save pathProgrammer productivity features include:IntelliSenseCode snippets321

  • Building and Running a .NET Framework ApplicationVisual StudioIn Visual Studio 2010, on the Build menu, click Build SolutionOn the Debug menu, click Start Debugging12

  • Demonstration: Disassembling a .NET Framework AssemblyIn this demonstration, you will: Run an existing .NET Framework application Open Ildasm Disassemble an existing .NET Framework assembly Examine the disassembled .NET Framework assembly

  • Notes Page Over-flow Slide. Do Not Print Slide. See Notes pane.

  • Lesson 3: Writing a C# Application What Are Classes and Namespaces?The Structure of a Console ApplicationPerforming Input and Output by Using a Console ApplicationBest Practices for Commenting C# Applications

  • What Are Classes and Namespaces? System.IO namespaceA class is essentially a blueprint that defines the characteristics of an entityA namespace represents a logical collection of classes File classPath classDirectoryInfo classDirectory classFileInfo class

  • The Structure of a Console Applicationusing System;namespace MyFirstApplication{ class Program { static void Main(string[] args) { } }}

    Bring System namespace into scopeProgram class declarationMain method declarationNamespace declaration

  • Performing Input and Output by Using a Console ApplicationReadLine()Clear()ReadKey()Write()WriteLine()Read()System.Console method includes:using System;...Console.WriteLine("Hello there!);

  • Best Practices for Commenting C# Applications// This is a comment on a separate line.string message = "Hello there!"; // This is an inline comment.Begin procedures by using a comment blockIn 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 write a decision structure, use a comment to indicate how the decision is made and what it implies

  • Lesson 4: Building a Graphical ApplicationWhat Is WPF?The Structure of a WPF ApplicationThe WPF Control LibraryWPF EventsBuilding a Simple WPF ApplicationDemonstration: Building a Simple WPF Application

  • What Is WPF? WPF is a new foundation for building Windows-based applications by combining:MediaDocumentsGraphical user interface Features of WPFEase of user interface designExtensive support for client application developmentUse of XAMLSupport for interoperability with older applications

  • The Structure of a WPF Application

    Declarative XAML filenamespace WpfApplication1{ public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } }}Code-behind Visual C# file

  • The WPF Control LibraryButtonWPF controls include:CanvasComboBoxGridLabelStackPanelTextBox

    Click Me

    Button example:

  • WPF EventsClickMeButton definitionprivate void myButton_Click(object sender, RoutedEventArgs e){ // Code to do something goes here.}Event handlerUsing WPF, you create event-driven applications, for example, responding to a button being clicked, item selections, and so on

  • Building a Simple WPF Application Visual Studio enables you to:Create a new WPF applicationAdd controls to the WPF applicationSet control propertiesAdd event handlers to controlsAdd code to implement business logic12345

  • Demonstration: Building a Simple WPF ApplicationIn 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

  • Notes Page Over-flow Slide. Do Not Print Slide. See Notes pane.

  • Lesson 5: Documenting an ApplicationWhat Are XML Comments?Common XML Comment TagsGenerating Documentation from XML Comments

  • What Are XML Comments?/// The Hello class prints a greeting on the screen /// public class Hello{ /// We use console-based I/O. For more information /// about /// WriteLine, see /// public static void Main( ) { Console.WriteLine("Hello World"); }}Use XML comments to generate Help documentation for your applications

  • Common XML Comment Tags Common tags include:

  • Generating Documentation from XML CommentsGenerate an XML file from Visual Studio 2010

    MyProject The Hello class prints a greeting on the screen ...

  • Lesson 6: Debugging Applications by Using Visual Studio 2010Debugging in Visual Studio 2010Using BreakpointsStepping Through and Over CodeUsing the Debug Windows

  • Debugging in Visual Studio 2010Debugging is an essential part of application developmentVisual Studio 2010 provides several tools to help you debug codeStep OutStep OverStep IntoRestartStop DebuggingBreak AllStart Debugging

  • Using BreakpointsWhen you run an application in Debug mode, you can pause execution and enter break mode Visual Studio 2010 enables you to:Locate a specific line of code and set a breakpointLocate a breakpoint and disable itLocate a breakpoint and remove it

  • Stepping Through and Over CodeYou can step through code one statement at a time to see exactly how processing proceeds through your application Visual Studio 2010 enables you to:Step into the current statementStep over the current statementStep out of the current statement

  • Using the Debug WindowsVisual Studio 2010 includes several windows that you canuse to help debug your applicationsLocalsOutputMemoryProcessesModulesCall StackQuickWatchThreadsImmediate

  • Exercise 1: Building a Simple Console ApplicationExercise 2: Building a WPF ApplicationExercise 3: Verifying the ApplicationExercise 4: Generating Documentation for an Application

    Logon informationEstimated time: 60 minutesLab: Introducing C# and the .NET Framework

  • Lab Scenario

  • Lab ReviewReview QuestionsWhat 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?

  • Module Review and TakeawaysReview QuestionsBest PracticesTools

  • Notes Page Over-flow Slide. Do Not Print Slide. See Notes pane.

    Presentation: 60 minutesLab: 60 minutes

    After completing this module, students will be able to:Describe the key features of the Microsoft .NET Framework 4.Describe how to create projects by using Microsoft Visual Studio 2010.Describe how to build a console application by using Microsoft Visual C#.Describe how to build an application with a graphical user interface.Describe how to document an application.Describe how to run and debug an application by using Visual Studio 2010.

    Required materialsTo teach this module, you need the Microsoft Office PowerPoint file 10266A_01.ppt.

    Important: It is recommended that you use Office PowerPoint 2002 or a later version to display the slides for this course. If you use Office PowerPoint Viewer or an earlier version of Office PowerPoint, all the features of the slides might not be displayed correctly.

    Preparation tasksTo prepare for this module:Read all of the materials for this module.Practice performing the demonstrations and the lab exercises.Work through the Module Review and Takeaways section, and determine how you will use this section to reinforce student learning and promote knowledge transfer to on-the-job performance.

    Make sure that students are aware that there are additional information and resources for the module on the Course Companion CD.Module 1: Introducing C# and the .NET Framework

    Course 10266A*Module 1: Introducing C# and the .NET FrameworkCourse 10266A*Explain that in this module you will introduce students to Microsoft Visual Studio 2010 and the .NET Framework 4, and how it provides a comprehensive development platform to enable you to build, debug, deploy, and manage applications.Module 1: Introducing C# and the .NET FrameworkCourse 10266A*Explain that this lesson introduces the .NET Framework 4, and describes the key concepts of .NET and some of the tools that are provided to help simplify development.Module 1: Introducing C# and the .NET FrameworkCourse 10266A*Explain the .NET Framework 4 provides a comprehensive development platform that offers a fast and efficient way to build applications and services.Explain that the. NET Framework 4 provides three principal elements:CLR - Explain that the CLR manages the execution of code and simplifies the development process by providing a robust and secure execution environmentClass Library Explain that the Class library provides a foundation of common functionality and constructs that help simplify application development and remove the requirement for developers to constantly reinvent logic.Development frameworks Explain that the development frameworks provide the necessary components and infrastructure to get you started. The development frameworks include: ASP.NET, Windows Communication Foundation (WCF), Windows Presentation Foundation (WPF), and Windows Workflow Foundation (WF).

    Question: What is the purpose of the .NET Framework 4, and the three main components that it provides?Answer: The .NET Framework 4 provides a comprehensive development platform that offers a fast and efficient way to build applications and services. The .NET Framework 4 consists of three components: the common language runtime, the .NET Framework class library, and development frameworks.Module 1: Introducing C# and the .NET FrameworkCourse 10266A*Explain the .NET Framework 4 provides a comprehensive development platform that offers a fast and efficient way to build applications and services.Explain that the. NET Framework 4 provides three principal elements:CLR - Explain that the CLR manages the execution of code and simplifies the development process by providing a robust and secure execution environmentClass Library Explain that the Class library provides a foundation of common functionality and constructs that help simplify application development and remove the requirement for developers to constantly reinvent logic.Development frameworks Explain that the development frameworks provide the necessary components and infrastructure to get you started. The development frameworks include: ASP.NET, Windows Communication Foundation (WCF), Windows Presentation Foundation (WPF), and Windows Workflow Foundation (WF).

    Question: What is the purpose of the .NET Framework 4, and the three main components that it provides?Answer: The .NET Framework 4 provides a comprehensive development platform that offers a fast and efficient way to build applications and services. The .NET Framework 4 consists of three components: the common language runtime, the .NET Framework class library, and development frameworks.Module 1: Introducing C# and the .NET FrameworkCourse 10266A*Explain that C# is the language of choice for many .NET developers, but not the only language, for example, Visual Basic .NET.Explain that C# uses a very similar syntax to C, C++, and Java, and is therefore easy to learn.Explain that C# has been standardized and that other vendors provide C# compilers.

    Question: Which programming languages have you used?Answer: Ask students which languages they have used.Module 1: Introducing C# and the .NET FrameworkCourse 10266A*Explain that C# is the language of choice for many .NET developers, but not the only language, for example, Visual Basic .NET.Explain that C# uses a very similar syntax to C, C++, and Java, and is therefore easy to learn.Explain that C# has been standardized and that other vendors provide C# compilers.

    Question: Which programming languages have you used?Answer: Ask students which languages they have used.Module 1: Introducing C# and the .NET FrameworkCourse 10266A*Explain what an assembly is, mentioning that assemblies form the fundamental unit of deployment, version control, reuse, activation scoping, and security permissions.Explain that an assembly can be either a .dll or an .exe file.Explain that you should version any assemblies that you distribute. Explain that versioning information can help you identify which versions customers already have and enable you to perform the necessary steps to upgrade the application.Explain that as well as versioning you should also sign your assemblies. Explain that signing ensures that your assemblies cannot easily be modified or replaced by an alternative implementation from a malicious source.

    Question: Why would you choose to distribute an assembly rather than distribute the source code?Answer: An assembly is a compiled unit that can contain multiple functional components that is ready to run. If you distributed raw source files, end users would have to compile the code before they could run the application.Module 1: Introducing C# and the .NET FrameworkCourse 10266A*Explain what an assembly is, mentioning that assemblies form the fundamental unit of deployment, version control, reuse, activation scoping, and security permissions.Explain that an assembly can be either a .dll or an .exe file.Explain that you should version any assemblies that you distribute. Explain that versioning information can help you identify which versions customers already have and enable you to perform the necessary steps to upgrade the application.Explain that as well as versioning you should also sign your assemblies. Explain that signing ensures that your assemblies cannot easily be modified or replaced by an alternative implementation from a malicious source.

    Question: Why would you choose to distribute an assembly rather than distribute the source code?Answer: An assembly is a compiled unit that can contain multiple functional components that is ready to run. If you distributed raw source files, end users would have to compile the code before they could run the application.Module 1: Introducing C# and the .NET FrameworkCourse 10266A*Explain the following :The Class Loader locates and loads all assemblies that the application requires. The Microsoft intermediate language (MSIL)-to-native compiler verifies the MSIL code and compiles all assemblies into machine code ready for execution.The Code Manager loads the executable assembly and runs the Main method.The Garbage Collector provides automatic lifetime memory management of all objects that your application creates. The Garbage Collector disposes of any objects that your application is no longer using.The Exception Manager provides structured exception handling for .NET applications, which is integrated with Windows Structured Exception Handling.

    Question: What steps does the CLR perform when you run your application?Answer: The CLR performs the following steps:The Class Loader locates and loads all assemblies that the application requires. The assemblies will already be compiled into MSIL.The MSIL-to-native compiler verifies the MSIL code and then compiles all assemblies into machine code ready for execution.The Code Manager loads the executable assembly and runs the Main method.

    Module 1: Introducing C# and the .NET FrameworkCourse 10266A*Explain what an assembly is, mentioning that assemblies form the fundamental unit of deployment, version control, reuse, activation scoping, and security permissions.Explain that an assembly can be either a .dll or an .exe file.Explain that you should version any assemblies that you distribute. Explain that versioning information can help you identify which versions customers already have and enable you to perform the necessary steps to upgrade the application.Explain that as well as versioning you should also sign your assemblies. Explain that signing ensures that your assemblies cannot easily be modified or replaced by an alternative implementation from a malicious source.

    Question: Why would you choose to distribute an assembly rather than distribute the source code?Answer: An assembly is a compiled unit that can contain multiple functional components that is ready to run. If you distributed raw source files, end users would have to compile the code before they could run the application.Module 1: Introducing C# and the .NET FrameworkCourse 10266A*Explain the purpose of each tool on the slide and provide examples of when you could use them.Caspol - Enables users to modify the machine, user, and enterprise security policy.MakeCert - Enables users to create x.509 certificates for use in their development environment.Gacutil - Enables users to manipulate the assemblies in the GAC.Ngen - Enables users to improve the performance of .NET applications. Ildasm - Enables users to manipulate assemblies i.e. disassembling an assembly to view MSIL code.Sn - Enables users to sign assemblies with strong names.Show students the location of tools in:C:\Windows\Microsoft.NET\Framework\v2.0.50727C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin

    Question: You have created two applications that both use an assembly called Contoso.ReportGenerator.dll. Both applications will run on the same machine. What is the best approach to share the Contoso.ReportGenerator.dll assembly and which tool would you use?Answer: You would install the assembly into the Global Assembly Cache (GAC) by using the Gacutil.exe tool.Module 1: Introducing C# and the .NET FrameworkCourse 10266A*Explain that this lesson introduces students to Visual Studio 2010 and describes how it can help simplify the development of .NET applications with:Predefined application templates.Features of the integrated development environment (IDE).

    Module 1: Introducing C# and the .NET FrameworkCourse 10266A*Briefly explain that Visual Studio provides the following features:Intuitive integrated development environment (IDE). Rapid application development with Designer and Code Editor windows, and wizards.Server and data access with the Server Explorer window.Debugging features.Error handling.Help and documentation, including Microsoft IntelliSense and code snippets.

    If time permits, show students Visual Studio 2010.

    Question: What are the main reasons why you may choose Visual Studio 2010 over a text editor such as Notepad++?Answer: Answers should include:The intuitive IDE in Visual Studio 2010. Visual Studio 2010 supports rapid application development with Designer and Code Editor windows, and wizards.Visual Studio 2010 provides debugging features that help you fix any bugs in your code.Visual Studio 2010 offers help and support with IntelliSense, code snippets, and the Visual Studio community.Module 1: Introducing C# and the .NET FrameworkCourse 10266A*Briefly explain each of the templates highlighted on the slide, explaining their purpose and when you may want to use them.

    Question: What project templates would you use for each of the following:A client application that will run on a Windows-based computer.A library of functionality that you want to use in other applications.A Web site that you will host on an Internet Information Services (IIS) Web server.

    Answer: Possible answers include:For a client application, you could use the WPF Application and Windows Forms Application templates.For a library, you could use the Class Library template.For a Web site, you could use the ASP.NET Web Application and ASP.NET MVC 2 Application templates.Module 1: Introducing C# and the .NET FrameworkCourse 10266A*Explain that Visual Studio 2010 uses solutions and projects as conceptual containers to organize your source files during development.Explain that a project is used to organize source files, references, and project-level configuration settings that make up a single .NET Framework application or library.Explain that a project typically includes .cs .csproj .aspx .config and .xaml files.Explain that a single Visual Studio solution is a container for one or more projects. Explain that a solution typically contains .sln and .suo files.If time permits, open Visual Studio and show students how to add and remove projects from a solution by using the Solution Explorer window.

    Question: What role does the .sln file play in Visual Studio solutions?Answer: .NET solutions act as a wrapper for solution settings and your .NET projects. .NET solutions also enable you to build multiple projects without having to open multiple instances of Visual Studio.Module 1: Introducing C# and the .NET FrameworkCourse 10266A*Explain the steps on the slide and show students the New Project dialog box in Visual Studio 2010.Explain and demonstrate IntelliSense and code snippets.

    Question: What is the purpose of code snippets?Answer: Code snippets remove the need for developers to repeatedly type common code constructs.Module 1: Introducing C# and the .NET FrameworkCourse 10266A*Explain that you can build and run an application in Visual Studio.Show students both the Build menu and Debug menu in Visual Studio.Explain that you can also build your application on the command line by using csc.exe.Explain the contents of the command using the example on the slide./t:/out:The last parameter being the location of the source files you want to compile.

    Question: Describe two ways to build and run a .NET Framework application.Answer: You can build a .NET application by using Visual Studio or csc.exe on the command line.In this demonstration, you will see how to use Ildasm.exe to disassemble a .NET assembly.

    Demonstration StepsTask 1: Run an existing .NET applicationRight-click the Start menu, and then click Open Windows Explorer.In Windows Explorer, move to the E:\Demofiles\Mod1\Demo1 folder, and then double-click MyFirstApplication.exe.In the MyFirstApplication window, point out:The title of the Command Prompt window, which is set to My First Application.The text Hello, this is my first .NET application which is displayed in the Command Prompt window.That the Command Prompt window does not automatically close until you click the X icon on the toolbar.Close the MyFirstApplication window.

    Task 2: Open IldasmRight-click the Start menu, and then click Open Windows Explorer.In Windows Explorer, move to the C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin folder, and then double-click ildasm.exe.

    Task 3: Disassemble an existing .NET Framework assemblyIn the Il Dasm window, on the File menu, click Open.In the Open dialog box, move to the E:\Demofiles\Mod1\Demo1 folder, and then double-click MyFirstApplication.exe.

    Module 1: Introducing C# and the .NET FrameworkCourse 10266A*Task 4: Examine the disassembled .NET Framework assemblyIn the Il Dasm window, in the output pane, point to the Manifest node and explain that this is stored alongside the assembly.Double-click the Manifest node.In the Manifest window, show students the following, and then close the Manifest window:.publickeytokenexplain that this is the public key for the assembly..verexplain that this is the version of the assembly.Expand the MyFirstApplication node, and then point to the MyFirstApplication.Program class. Explain that this is the only class in the MyFirstApplication assembly.Expand the MyFirstApplication.Program node.Point to the .ctor : void() node, and then explain that this is the default constructor for the MyFirstApplication.Program class.Point to the Main : void(string[]) node, and then explain that this is the default Main method provided in the MyFirstApplication.Program class, and is the entry point into the application. Inform students that the concept of the Main method will be covered in the next lesson.Double-click the Main : void(string[]) node.In the MyFirstApplication.Program::Main : void(string[]) window, show students the following, and then close the MyFirstApplication.Program::Main : void(string[]) window:void [mscorlib]System.Console::set_Title(string)explain that this is a call to set the title of the Command Prompt window to the text My First Application.void [mscorlib]System.Console::WriteLine(string)explain that this is a call to display the text Hello, this is my first .NET application in the Command Prompt window.string [mscorlib]System.Console::ReadLine()explain that this is a call to stop the Command Prompt window from closing until the user presses a key.On the File menu, click Exit.

    Question: When developing a .NET Framework application, how would you find Ildasm useful? Answer: It is very useful to try to understand the inner workings of .NET, especially when you are developing your first .NET application. You can use Ildasm to inspect assemblies that you build and see how the compiler generates MSIL code.Course 10266A*Module 1: Introducing C# and the .NET FrameworkModule 1: Introducing C# and the .NET FrameworkCourse 10266A*Explain that this lesson describes the following:Structure of a simple C# application, and how a C# application contains one or more classes. How to reference functionality that is defined in classes in other assembles and libraries, and how you can use the Console class in the .NET Framework class library to perform simple input and output operations. How and why you should add comments to your applications.Module 1: Introducing C# and the .NET FrameworkCourse 10266A*Explain the purpose of a class, mentioning that a class is essentially a blueprint that defines the characteristics of an entity.Explain the purpose of a namespace, mentioning that a namespace represents a logical collection of classes.Explain that the .NET class library provides many namespaces and classes. Give the example of the System.IO namespace and some of its classes.Explain how to bring namespaces into scope, and that you can achieve this by using the using statement, or explicitly when using the class. If time permits, show this in Visual Studio.

    Question: In your console application, you want to use the Console class, which is part of the System namespace. How do you bring the System namespace into scope?Answer: You could use the using statement to bring the System namespace into scope.Module 1: Introducing C# and the .NET FrameworkCourse 10266A*Explain the code example on the slide, pointing out the namespace declaration, class declaration, and Main method stub.

    Question: In your console application, you have a method called Main. What is the purpose of the Main method?Answer: The Main method provides an entry point into the console application.

    Module 1: Introducing C# and the .NET FrameworkCourse 10266A*Explain each of the methods on the slide, giving examples of when each might be used.

    Question: Which two methods would you use to do the following:Display the message "Please press any key" on a new line.Capture the key that the user pressed.

    Answer: The correct answer is:Console.WriteLine("Please press any key);ConsoleKeyInfo keyPressed = Console.ReadKey();Module 1: Introducing C# and the .NET FrameworkCourse 10266A*Explain the best practices on the slide, and add any that you feel are valid.If time permits, show students how to create simple comments in Visual Studio.

    Question: Why is it important for you to comment your code?Answer: Answers should include:To improve the readability of your code.To capture the rationale behind your logic.To explain the purpose of elements in your code.Module 1: Introducing C# and the .NET FrameworkCourse 10266A*Explain that this lesson introduces students to applications that have a graphical user interface, and provides the example of a WPF application. Explain that you will also show student how WPF applications are structured, and how you can create your own WPF applications by using Visual Studio 2010.Module 1: Introducing C# and the .NET FrameworkCourse 10266A*Explain that Windows Presentation Foundation is the unified graphical subsystem for Windows that provides the foundation for building applications and high-fidelity experiences. Explain the main features of WPF, mentioning the following:Developers can create comprehensive, eye-catching applications.WPF provides a set of built-in controls.Use of XAML.Interoperability.

    Question: Why would you choose to use WPF to create an application instead of Windows Forms?Answer: WPF enables you to create applications with much more compelling user interfaces, and offers greater control over user interface components.Module 1: Introducing C# and the .NET FrameworkCourse 10266A*Explain the files typically included in a WPF project, mentioning, .xaml, .xaml.cs, and .config files.Explain how WPF uses XAML files and code-behind C# files to separate UI elements from logic modeled with managed code.Explain that the markup defines the UI elements, such as window title, width, and height. You can change these properties by editing the XAML code, or by using the Properties window in Visual Studio. Explain that the code behind file contains event handlers that respond to users interactions with the UI elements.

    Question: Can you think of any other markup languages that behave in a similar way to XAML?Answer: Web applications that use HTML.Module 1: Introducing C# and the .NET FrameworkCourse 10266A*Explain that the WPF control library provides many controls that you would find in a typical Windows application.Explain the syntax of the Button code example, including the fact that it is XML-based, its attributes, and so on.Explain that you can also define controls programmatically in Visual C#.

    Question: You are building a simple form to capture user credentials and enable users to log on. Which controls could you use to build this form?Answer: Answers could include:For layout, use a Grid control.For labels for the user name and password, use Label controls.For input boxes for the user name and password, use TextBox controls.For a Submit button, use a Button control.Module 1: Introducing C# and the .NET FrameworkCourse 10266A*Explain what is meant by an event-driven application.Provide examples of events, for example, a button click, selection of a list item, a change to text, and so on.Explain that you can specify the events that a control responds to at design time by editing the XAML definition of a control, or by using the Properties window.Explain the that you must provide methods that handle the events by using code in the code-behind file, use the example on the slide to illustrate a button click event handler.

    Question: When you develop your WPF applications, what two ways can you use to specify events for controls?Answer: Declaratively by using XAML markup, or imperatively in Visual C# code.Module 1: Introducing C# and the .NET FrameworkCourse 10266A*Briefly explain the steps on the slide. Dont dwell too much on this topic because the following demonstration shows students how to perform each of these steps.

    Question: What windows in Visual Studio 2010 do you typically use when you are building your applications?Answer: Answers should include:Solution ExplorerPropertiesXAMLDesignCode EditorIn this demonstration, you will see how to create a simple WPF application in Visual Studio 2010.

    This demonstration contains solution files that you can find at E:\Demofiles\Mod1\Demo2\Solution.

    Demonstration StepsTask 1: Create a new WPF applicationOn the Start menu, point to All Programs, click Microsoft Visual Studio 2010, and then click Microsoft Visual Studio 2010.In Visual Studio 2010, on the File menu, click New, and then click Project.In the New Project dialog box, perform the following tasks, and then click OK:In the center pane, click WPF Application.In the Name box, type MyFirstWpfAppIn the Location box, type E:\Demofiles\Mod1\Demo2\Starter

    Task 2: Add controls to the WPF applicationOn the View menu, click Toolbox.In the Toolbox window, double-click Button.Show students the control in the Design window, and the XAML markup in the XAML window.

    Task 3: Set the properties for the controlsIn the Design window, click the Button control.In the Properties window, on the Properties tab, set the following properties:Content: ClickMeButtonFontSize: 20Height: 50Width: 150In the XAML window, perform the following:In the Button element, set the Content attribute to Click Me.In the Window element, set the Height attribute to 150.In the Window element, set the Width attribute to 190.

    Module 1: Introducing C# and the .NET FrameworkCourse 10266A*Task 4: Add code to the applicationIn the Design window, click the Button control.In the Properties window, on the Events tab, show students the different types of events that you can handle for a Button control, and then double-click Click.In the Code Editor window for the MainWindow.xaml.cs file, in the ClickMeButton_Click method, add the code in the following code example....private void ClickMeButton_Click(object sender, RoutedEventArgs e){ MessageBox.Show("You clicked me!!");}...Explain to students that you have just added code so that, when the button is clicked, a message box is displayed.

    Task 5: Build and run the applicationOn the Build menu, click Build Solution.On the Debug menu, click Start Debugging.In the MainWindow window, click Click Me, and then click OK.On the Debug menu, click Stop Debugging.

    Question: When you are developing a WPF application in Visual Studio 2010, what are the two main ways in which you can set properties for WPF controls? Answer: You can set properties by using the Properties window, or by editing the XAML for the control directly by using the XAML window.Course 10266A*Module 1: Introducing C# and the .NET FrameworkModule 1: Introducing C# and the .NET FrameworkCourse 10266A*Explain that this lesson introduces XML comments and explains how you can use them when you are developing your .NET applications. This lesson also shows how to build a formatted help file by using the Sandcastle tool.

    Explain that XML comments enable you to generate documentation on your application programming interfaces (APIs).Explain that inline comments are part of the Visual C# standard, whereas XML comments are a Microsoft extension.Explain the syntax on the slide, with emphasis on /// and the tag.

    Question: Why would you use XML comments rather than standard comments?Answer: XML comments enable you to define comments in a more structured way. You can extract XML by using tools such as Sandcastle Help File Builder.Module 1: Introducing C# and the .NET FrameworkCourse 10266A*Explain the tags listed on the slide, mentioning the following: - brief description - long description - example of how the member should be used - specify application code return value of the member Question: Which tag would you use to provide a detailed description of a method?Answer: The tag.Module 1: Introducing C# and the .NET FrameworkCourse 10266A*Explain how you can instruct Visual Studio 2010 to produce an XML file.Explain how you can instruct csc.exe to produce an XML file.Explain the contents of the XML file.Explain that you can then consume this XML file by using a tool such as Sandcastle Help File Builder to produce a .chm file.

    Question: Which switch do you need to provide to get csc.exe to produce XML output?Answer: The /doc switch.Module 1: Introducing C# and the .NET FrameworkCourse 10266A*Module 1: Introducing C# and the .NET FrameworkCourse 10266A*Explain that this lesson will show you how to use Visual Studio 2010 to help you debug your applications, with features such as the Debug toolbar, breakpoints, and debug windows.Module 1: Introducing C# and the .NET FrameworkCourse 10266A*Explain that debugging is an essential part of application development.Explain if you dont detect error during development, then users may report these errors to you, and you will have to correct them.Explain that Visual Studio 2010 provides several tools to help you debug code, for example the ability to run your applications in debug mode.Explain that Visual Studio 2010 provides the debug toolbar and the debug menu, which provides a number of debug functions (briefly go through the list on the slide)

    Question: What are some of the debug functions that Visual Studio 2010 provides?Answer: Answers should include:Start/stop debugging.Halt on a breakpoint.Module 1: Introducing C# and the .NET FrameworkCourse 10266A*Explain that when you run an application in Debug mode, you can pause execution and enter break mode.Explain that in break mode, no further execution takes place until you restart the application or step through the code line by line. Open Visual Studio 2010, and then show students how to set, disable, and remove a breakpoint.

    Question: How would you use the debug functions in Visual Studio 2010 to debug your application and pause on a specific line of code? Answer: Answers should include:Locate the line of interest and set a breakpoint.Start the application with debugging.Module 1: Introducing C# and the .NET FrameworkCourse 10266A*Explain that you can step through code one statement at a time to see exactly how processing proceeds through your application. Open Visual Studio 2010, and then show students how to step over and into a simple method.

    Question: Why would you use the Step into and Step over debug functions? Answer: Answers should include:You would use Step into if you wanted to investigate the behavior in a particular method.You would use Step over if you didnt want to investigate the behavior of a method, but wanted to skip to the next line.Module 1: Introducing C# and the .NET FrameworkCourse 10266A*Explain that Visual Studio 2010 includes several windows that you can use to help debug your applications. Open Visual Studio 2010, and then show students each window.

    Question: Why would you use the Locals and Immediate windows when developing your application? Answer: Answers should include:You would use the Locals window to view and edit local (in-scope) variables.You would use the Immediate window to evaluate expressions, execute statements, and print out variable values.Module 1: Introducing C# and the .NET FrameworkCourse 10266A*In this lab, students will create simple console and WPF solutions to get started with using Visual Studio 2010 and C#. Students will also configure projects, use code-editing features, and create comments. They will become familiar with the debugger interface. Students will compile, run, and use the debugger to step through a program. Finally, students will generate documentation for an application.

    ImportantThe course prerequisites state that students are expected to have a programming background, preferably from a C, C++, or Java environment. Apart from introducing students to the Visual Studio environment, this set of labs is intended to be a warm-up, to enable the instructor to establish which students have good programming experience and which students do not. Students who have this experience should be able to tackle the exercises in this lab without too much reference to the lab answer key. Students who have less experience may need to work from the lab answer key more directly for this set of exercises.

    Exercise 1In this exercise, students will test the application by using console I/O. Students will then use I/O redirection to run the application by using data that is held in a file and verify that the results are as expected.Exercise 2In this exercise, students will initially test the display formatting by providing fields that the user can type data into. When students are satisfied that the display format is correct, they will modify the application to read input from the console and modify the Debug properties of the application to redirect this input to come from the same file as before.Exercise 3In this exercise, students will generate some additional test data and use it as input to their application. Students will use the Visual Studio 2010 debugger to step through their code and examine it as it runs.Exercise 4In this exercise, students will add XML comments to their application, and use the Sandcastle tool to generate documentation for the application.

    Before the students begin the lab, read the scenario associated with each exercise to the class. This will reinforce the broad issue that the students are troubleshooting and will help to facilitate the lab discussion at the end of the module. Remind the students to complete the discussion questions after the last lab exercise.

    Note: The lab exercise answer keys are provided on the Course Companion CD. To access the answer key, click the link located at the bottom of the relevant lab exercise page.Module 1: Introducing C# and the .NET FrameworkCourse 10266A*Explain the lab scenario to students.Fabrikam, Inc. produces a range of highly sensitive measuring devices that can repeatedly measure objects and capture data. You have been asked to write a C# application to read a small set of input data that a measuring device has generated, format this data to make it more readable, and then display the formatted results.The data consists of text data that contains pairs of numbers representing x-coordinates and y-coordinates of the location of an object. Each line of text contains one set of coordinates. The following code example resembles a typical dataset.23.8976,12.321825.7639,11.946324.8293,12.2134You have been asked to format the data like the following code example.x:23.8976 y:12.3218x:25.7639 y:11.9463x:24.8293 y:12.2134Module 1: Introducing C# and the .NET FrameworkCourse 10266A*Use the questions on the slide to guide the debriefing after students have completed the lab exercises.

    Question: What methods did you use to capture and display information in your console application?Answer: The static ReadLine and WriteLine methods.

    Question: What event did you handle on the Format Data button in your WPF application?Answer: The Click event handler.

    Question: What debugging functions did you use when you verified the application?Answer: Start Debugging, breakpoint, Step into, Step over, and the Immediate window.

    Question: How do you instruct Visual Studio 2010 to produce an XML file that contains XML comments?Answer: Set the XML documentation file property.Module 1: Introducing C# and the .NET FrameworkCourse 10266A*Review QuestionsPoint the students to the appropriate section in the course so that they are able to answer the questions presented in this section.

    Question: What is the purpose of the .NET Framework and the role of Visual C#? Answer: The .NET Framework 4 provides a comprehensive development platform that offers a fast and efficient way to build applications and services. C# is the language of choice for many developers. It uses a very similar syntax to C, C++, and Java, with several extensions and features designed for operation with the .NET Framework.

    Question: What is the purpose of Visual Studio 2010 templates? Answer: Visual Studio 2010 supports the development of different types of applications such as Windows-based client applications, Web-based applications, services, and libraries. To help you get started, Visual Studio 2010 provides several application templates that provide a structure for the different types of applications.

    Question: What is the purpose of Visual Studio projects and solutions?Answer: A project is used to organize source files, references, and project-level configuration settings that make up a single .NET Framework application or library. A single Visual Studio solution is a container for one or more projects.

    Question: What is the purpose of a Main method?Answer: Every .NET Framework application that compiles into an executable file must have a Main method. This method provides the CLR with an entry point into the application. When you run a .NET Framework application, the Main method is the first method that the CLR executes.

    Question: List some of the controls that WPF provides.Answer: Any of Button, Canvas, ComboBox, Grid, Label, StackPanel, and TextBox.

    Question: What is the purpose of XML comments?Answer: In Visual Studio 2010, you can add comments to your source code that will be processed to an XML file. This file can then be the input to a process that creates Help documentation for the classes in your code.

    Question: What is the purpose of the Visual Studio 2010 debugger?Answer: Debugging is an essential part of application development. You may notice errors as you write code, but some errorsespecially logic errorsmay only occur in specific circumstances that you do not test for. Users may report these errors to you, and you will have to correct them. Visual Studio 2010 provides several tools to help you debug code.

    Best Practices Related to Writing a C# ApplicationHelp the students understand the best practices presented in this section. Ask students to consider these best practices in the context of their own business situations:Keep the Main method small and lightweight.Declare variables by using meaningful names and avoid reference to the underlying data type, for example, nameString.Define controls by using meaningful names and avoid reference to the underlying control type, for example, labelName.Add comments to your code that describe your thought process.

    ToolsPoint out the location from which each key tool can be installed. Let students review the function and usage of each tool on their own. Remind students that they can use this as a master list to help them gather all the tools required to facilitate their application support work:Caspol.exe (C:\Windows\Microsoft.NET\Framework\v2.0.50727)Gacutil.exe (C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin)Ildasm.exe (C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin)Makecert.exe (C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin)Ngen.exe (C:\Windows\Microsoft.NET\Framework\v2.0.50727)Sn.exe (C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin)Course 10266A*Module 1: Introducing C# and the .NET Framework