Академический Документы
Профессиональный Документы
Культура Документы
2609A
Introduction to C# Programming with
Microsoft® .NET
Companion Content
Information in this document, including URL and other Internet Web site references, is subject to change
without notice. Unless otherwise noted, the example companies, organizations, products, domain names,
e-mail addresses, logos, people, places, and events depicted herein are fictitious, and no association with
any real company, organization, product, domain name, e-mail address, logo, person, place or event is
intended or should be inferred. Complying with all applicable copyright laws is the responsibility of the
user. Without limiting the rights under copyright, no part of this document may be reproduced, stored in
or introduced into a retrieval system, or transmitted in any form or by any means (electronic, mechanical,
photocopying, recording, or otherwise), or for any purpose, without the express written permission of
Microsoft Corporation.
Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual property
rights covering subject matter in this document. Except as expressly provided in any written license
agreement from Microsoft, the furnishing of this document does not give you any license to these
patents, trademarks, copyrights, or other intellectual property.
The names of manufacturers, products, or URLs are provided for informational purposes only and
Microsoft makes no representations and warranties, either expressed, implied, or statutory, regarding
these manufacturers or the use of the products with any Microsoft technologies. The inclusion of a
manufacturer or product does not imply endorsement of Microsoft of the manufacturer or product. Links
may be provided to third party sites. Such sites are not under the control of Microsoft and Microsoft is not
responsible for the contents of any linked site or any link contained in a linked site, or any changes or
updates to such sites. Microsoft is not responsible for webcasting or any other form of transmission
received from any linked site. Microsoft is providing these links to you only as a convenience, and the
inclusion of any link does not imply endorsement of Microsoft of the site or the products contained
therein.
© 2002 Microsoft Corporation. All rights reserved.
Microsoft and the trademarks listed at
http://www.microsoft.com/about/legal/en/us/IntellectualProperty/Trademarks/EN-US.aspx are trademarks
of the Microsoft group of companies. All other marks are property oftheir respective owners.
Module 1
Introducing the .NET Platform
Contents:
Questions and Answers 2
Multimedia 4
1-2 Introduction to C# Programming with Microsoft® .NET
XML Web services A. Component that periodically checks for objects that are ready to be
released from a computer's memory.
.NET Framework D. Component that contains a collection of reusable types that you can use
to develop applications.
.NET Framework class library E. Programming model of the .NET platform for building, deploying, and
running XML Web services and all types of applications-both desktop and
Web-based.
Common language runtime F. Refers to a hand held computer or mobile telephone that can use .NET-
based applications.
Garbage collection G. Component that manages the execution of code and provides services
to make the development process easier.
MSIL H. Services, .NET-based applications, and Web sites that rely on XML Web
services to enhance the user experience.
Answer: XML Web services = C, Devices = F, .NET experiences = H, .NET Framework = E, .NET
Framework class library = D, Common language runtime = G, Garbage collection = A, MSIL = B
Review
1. Complete the following statement:
When you create a project in Visual Studio .NET, files are organized in a larger container called a .
Answer: When you create a project in Visual Studio .NET, project files are organized in a
larger container called a solution.
2. Draw a line to match the following file extensions with the correct description.
Extension Description
.cs A. Organizes projects, project items, and solution items in the solution.
.sln B. Records all of the options that you may associate with your solution.
.aspx C. Represents forms, user controls, classes, and module files that belong to a
single-project solution.
Introducing the .NET Platform 1-3
.suo D. Represents forms, user controls, classes, and module files that belong to a
multiple-project solution.
Answer: An application template provides starter files and a project structure and contains
the basic project objects and the environment settings that you need to create the type of
application that you want to build.
4. What must be installed on a client computer to run .NET-based applications?
Answer: The .NET runtime must be installed on a client computer to run .NET-based
applications.
5. What is one advantage of programming by using the .NET Framework versus using a traditional
development environment?
Answer: The .NET Framework allows the developer to write code in one operating system
and then to deploy the application on other computers running other operating systems.
1-4 Introduction to C# Programming with Microsoft® .NET
Multimedia
Media Type Title
Module 2
Understanding C# Language Fundamentals
Contents:
Questions and Answers 2
2-2 Introduction to C# Programming with Microsoft® .NET
1. Read the code in the right column, and a. Read the following code:
then answer the following question.
int x = 10;
int y = x++;
2. Read the code in the right column, and a. Read the following code, which is continued from
then answer the following question. the preceding step:
x += 10;
Answer: x == 21. x was incremented to 11 after being assigned to y in step 1 and has 10
added to it in step 2.
3. Read the code in the right column, and a. Read the following code, which is continued from
then answer the following question. the preceding step:
int z = 30;
int a = x + y * z;
4. Read the code in the right column, and a. Read the following code, which is continued from
then answer the following question. the preceding step:
int a = 10;
int b = a++;
bool myBool = ( a == b );
Answer: This code declares and initializes a to 10. Then b is initialized to 10 (because a
post-increments). It then compares a and b for equality. Because they are not equal, the
expression is false, and myBool is assigned the value false.
5. Start Visual Studio .NET, and then open a. Start a new instance of Visual Studio .NET.
install_folder\Practices \Mod02\Operators b. On the Start Page, click Open Project.
\Operators.sln.
c. In the Open Project dialog box, browse to
install_folder\Practices\Mod02\Operators, click
Operators.sln, and then click Open.
d. In Solution Explorer, click Form1.cs, and then press
F7 to open the Code Editor.
6. Check your answers by stepping through a. Locate the line int x = 10; and set a breakpoint at
the code. that line.
b. Press F5 to compile and run the application.
c. If the Locals window is not visible, in Visual Studio
.NET, on the Debug menu, point to Windows, and
then click Locals.
d. In your application window, click Run.
e. Step through your code, a line at a time, by clicking
the Step Over button, or by pressing F10.
f. Examine the Locals and Autos windows to check
that your application assigns values correctly.
Review
1. What symbol indicates a single-line comment in your code?
Answer: Two forward slashes (//).
2. True or false: You end a statement with a closing brace and a semicolon.
Answer: False. You do not need to include a semicolon after braces because the braces
themselves indicate a complete block of code.
3. What is the largest value that can fit in a byte?
Answer: 255
4. In the following expression, what is the value of y?
int x = 50;
int y =
++x;
Answer: Conditional
6. True or False: The while loop is a pre-test loop.
2-4 Introduction to C# Programming with Microsoft® .NET
Answer: True
Creating Objects in C# 3-1
Module 3
Creating Objects in C#
Contents:
Questions and Answers 2
Multimedia 3
3-2 Introduction to C# Programming with Microsoft® .NET
Answer: b. (Private) By default, a class member is accessible from within the containing class
only.
2. What is the keyword that is used to inform the compiler that a variable is initialized within a
method?
Answer: out
3. What is the purpose of overloading a constructor?
Multimedia
Media Type Title
Module 4
Implementing Object-Oriented Programming Techniques in
C#
Contents:
Questions and Answers 2
4-2 Introduction to C# Programming with Microsoft® .NET
Answer: A derived class should be more specialized than its base class. A base class should
provide generalized properties and actions.
3. What are some of the benefits of object-oriented programming?
Answer:
• Programs are easier to design because objects reflect real-world items.
• Applications are easier for users because data that they do not need is hidden.
Module 5
Programming with C#
Contents:
Questions and Answers 2
5-2 Introduction to C# Programming with Microsoft® .NET
number[2];
2. Create an array that contains the integers 1, 2, and 3. Then use the foreach statement to iterate
over the array and output the numbers to the console.
Answer:
3. Name two collection objects in System.Collections namespace, and describe how these classes
track objects.
Answer: The Queue class tracks objects on a first-in, first-out basis.
The Stack class tracks object on a first-in, last-out basis.
4. What is a delegate, what is the benefit of using a delegate, and when should you use it?
Answer: A delegate is a reference to a method. If a delegate is invoked, the method referred
to by the delegate is executed.
Delegates allow you to build scalability in your classes. You use a delegate when you want to
call a method, but you will not know what method until run time.
Building .NET-based Applications with C# 6-1
Module 6
Building .NET-based Applications with C#
Contents:
Questions and Answers 2
6-2 Introduction to C# Programming with Microsoft® .NET
1. Start Visual Studio .NET, and then create a a. Start a new instance of Visual Studio .NET.
new project. Project Type: Visual C# b. On the Start Page, click New Project.
Template: Windows Application Name:
c. In the New Project dialog box, under Project
ObjectBrowserPractice
Types, click Visual C# Projects.
d. Under Templates, click Windows Application.
e. In the Name box, type ObjectBrowserPractice
f. In the Location box, browse to
install_folder\Practices\Mod06 and then click OK.
2. Display the Object Browser. On the View menu, point to Other Windows, and
then click Object Browser.
Using the Object Browser, document the Equals method of the Object object. Include the access
modifiers in your documentation.
Answer: The Equals method has two forms, public static Equals(object,object) and public
virtual Equals(object).
Using the Object Browser, document how many implementations of the method Compare are
supported by the String object.
Answer: 6.
Using the Object Browser, find the Convert class and document the class modifiers that are listed
for the class. In your document, include what effect the modifier has on the class.
Answer: The class modifiers for the Convert class are public and sealed. Because the class is
sealed, it is not possible to derive a class from this class.
Using the Object Browser, find the ReadUInt16 method. What does this method do?
Answer: Reads a 2-byte unsigned integer from the current stream using little endian
encoding and advances the position of the stream by two bytes.
Using the Object Browser, find the ArrayList class. Can you set the IsReadOnly property to true
or false?
Answer: No, the Object Browser shows this property as being a GET property as opposed
to a SET GET property. You can read the value (GET) but not update the value (SET).
Using the Object Browser, find the FileStream class. What namespace contains this class?
Answer: System.IO.
Building .NET-based Applications with C# 6-3
Using the Object Browser, find the ReadUInt32 method. What does this method do?
Answer: Reads a 4-byte unsigned integer from the current stream and advances the
position of the stream by four bytes.
Review
1. The following table lists namespace contents and namespaces. Draw a line to match the namespace
to its contents.
2. What methods are inherited from the System.Object base class when you create a new class?
Answer: The ToString, GetHashCode, Equals and GetType methods are inherited from the
System.Object class.
3. The Append, AppendFormat, Insert, and Replace methods belong to which class?
Answer: The StringBuilder class.
4. What type of object do you create to open an existing file or to create a new file?
Answer: Create a FileStream object to create a new file, or to open an existing file.
5. Which two classes are used for writing and reading binary data?
Answer: The BinaryReader and BinaryWriter classes are used for writing and reading binary
data.
Using ADO.NET to Access Data 7-1
Module 7
Using ADO.NET to Access Data
Contents:
Questions and Answers 2
Multimedia 3
Lab: Question and Answers 4
7-2 Introduction to C# Programming with Microsoft® .NET
Multimedia
Media Type Title
What ADO.NET objects are created when you use Server Explorer to add the BankCustomers
table to your application?
2. Create a DataSet object and fill it with the a. In the Designer window, select sqlDataAdapter1
data from the BankCustomers table. and on the Data menu, click Generate Dataset.
b. Use the dialog box to add the DataSet object to
your application.
c. Write code to fill the DataSet object with the data
that is read by sqlDataAdapter1 by using
sqlDataAdapter1.Fill. You can place this line of
code in the Form1 constructor, after the call to
InitializeComponent.
3. Add a DataGrid to your form, and bind its a. Use the Toolbox to add a DataGrid to your form.
DataSource to the BankCustomers table b. Use the Properties window to set the DataGrid
in the DataSet object. DataSource property to the BankCustomers table
in the DataSet object.
4. Add a button to your form, label it a. Add a button to the form, changing its Text
Update, and then add a Click event property to Update.
handler that will call the data adapter b. Double-click the Update button and note that a
Update method. new method is added.
By default, this method is called button1_Click, and it is
called when the Update button is clicked.
c. In button1_Click, call the Update method of the
SqlDataAdapter object. For example:
sqlDataAdapter1.Update( dataSet11 );
5. Compile, run, and test your application. a. Press F5 to compile and run your application.
b. You can edit existing database entries and then
click Update to commit these changes to the
Using ADO.NET to Access Data 7-5
database.
c. Use Server Explorer to verify that your changes
have been made to the database.
d. You can also add new records if you specify a
unique CustomerID.
Module 8
Creating Windows-based Applications
Contents:
Questions and Answers 2
8-2 Introduction to C# Programming with Microsoft® .NET
Module 9
Using XML Web Services in a C# Application
Contents:
Questions and Answers 2
9-2 Introduction to C# Programming with Microsoft® .NET
Answer: A Class is defined as an XML Web service by adding the [WebService...] attribute to
the class definition.
3. When would you use an XML Web service?
Answer: An XML Web service is appropriate for applications that may need to send XML-
based messages over the Internet that provide specific functionality for a customer,
applications that require integration where the XML Web service exposes the functionality
and data of each application as an XML Web service, or workflow applications where the
XML Web service allows end-to-end workflow solutions to be created.
4. Why would you use an XML Web service in an internal network?
Answer: An XML Web service can be used in an internal network to support earlier
applications by exposing the functionality of the applications to programmers in an
organization.
Creating a Web Application with Web Forms 10-1
Module 10
Creating a Web Application with Web Forms
Contents:
Questions and Answers 2
Multimedia 3
10-2 Introduction to C# Programming with Microsoft® .NET
Review
1. How can you view all of the files that Visual Studio .NET creates for a new ASP.NET Web
application?
Answer: Click Show All Files in the toolbar of Solution Explorer.
2. What are the extensions for the two files that make up a Web Forms page?
Answer: The WebForm1.aspx file and the WebForm1.aspx.cs file.
3. What are the three components, not visible on a Web page, that are required to display the
contents of a SQL Server table in a DataGrid control?
Answer: SqlConnection object, SqlDataAdapter object, and DataSet object.
4. Name the five basic stages of a Web Forms page life cycle.
Answer: Page_Init, Page_Load, Validation, other event handling, and Page_Unload.
5. Name the five steps that are necessary to display data on a Web Forms page.
Answer: Create a Web application and Web Forms page, create the data connection and
data adapter, create the DataSet, add a DataGrid to display data, and fill the DataSet and
display data in the DataGrid control.
6. What browsers are supported by ASP.NET?
Answer: All browsers.
7. Can you have more than one Web.Config applications settings?
Answer: Yes, in different cascading folders.
Creating a Web Application with Web Forms 10-3
Multimedia
Media Type Title
Module 11
Application Settings and Deployment
Contents:
Questions and Answers 2
11-2 Introduction to C# Programming with Microsoft® .NET
1. Start Visual Studio and create a new project. a. Start Visual Studio.NET.
Name: Registry Project Type: Visual C# projects b. On the File menu, point to New, and then
Template: Windows Application Location: click Project.
install_folder\Practices\ Mod11
c. In the New Project window, under Project
Types, click Visual C# Projects.
d. Under Templates, click Windows
Application.
e. In the Name box, type Registry
f. In the Location box, type
install_folder\Practices\Mod11 and then
click OK.
2. Add a button to the form. Set the Text property a. Hover over the toolbox, drag a button
of the button to Add Info to the Registry. control from the toolbox and drop it onto
the form.
b. In the Properties window, click Text and
then type Add Info to the Registry.
3. Add the code below into the button click event. a. Double-click the button.
b. In the button1_Click event procedure, add
the code below.
CurrentUser.CreateSubKey("SOFTWARE\\MSDNTraining");
key.SetValue("TestData","ABCDEF");
key.Close();
4. Run the application and click the button. Close a. Click Start on the standard toolbar.
the window. b. Click the button on the form.
c. Close the window.
5. Open RegEdit and verify that the key a. Click Start, and then click Run.
HKEY_CURRENT_USER\Software\MSDNTraini b. In the Run dialog box, in the Open box, type
ng contains a value for TestData of ABCDEF. RegEdit and then click OK.
Close the Registry Editor window, close Visual
c. In the Registry Editor window, expand
Studio.
HKEY_CURRENT_USER, expand
SOFTWARE, and then click MSDNTraining.
d. Verify that the string value TestData
contains ABCDEF.
e. Close the Registry Editor window.
f. Close Visual Studio.
Application Settings and Deployment 11-3
The first line of the code added into the button click event could be shortened. How would you
accomplish this?
Answer: By including a using Microsoft.Win32 statement at the top of the Form1.cs file.
The occurrences of Microsoft.Win32 in the code statement could then be removed.
Review
1. What are some benefits of storing user preferences in a database?
Answer:
Central location
Global preferences
Regular backups
2. What are the four deployment templates that are available in Visual Studio .NET?
Answer:
Merge Module Project
Setup Project
Web Setup Project
Cab Project
Introduction to C# Programming with Microsoft® .NET R-1
Resources
Contents:
Internet Links 2
R-2 Introduction to C# Programming with Microsoft® .NET
Internet Links
The Web sites listed below provide additional resources.
Note Not all training products will have a Knowledge Base article – if that is the case, please ask your
instructor whether or not there are existing error log entries.
Courseware Feedback
Send all courseware feedback to support@mscourseware.com. We truly appreciate your time and effort.
We review every e-mail received and forward the information on to the appropriate team. Unfortunately,
because of volume, we are unable to provide a response but we may use your feedback to improve your
future experience with Microsoft Learning products.
Reporting Errors
When providing feedback, include the training product name and number in the subject line of your e-
mail. When you provide comments or report bugs, please include the following:
• Document or CD part number
• Page number or location
• Complete description of the error or suggested change
Please provide any details that are necessary to help us verify the issue.
Important All errors and suggestions are evaluated, but only those that are validated are added to the
product Knowledge Base article.