
C HAPTER 1 A Radically New Approach: C# and Windows Microsoft states that “C# is a simple, modern, object-oriented, and type-safe programming language derived from C and C++.” The first thing you will notice when using C# (C sharp) is how familiar you already are with many of the constructs of this language. Object-oriented by design, the C# language provides access to the class libraries available to Visual Basic and Visual C++ programmers. C#, however, does not provide its own class library. C# is implemented by Microsoft in the latest version of the Microsoft Visual Studio and provides access to the Next Generation Windows Services (NGWS). These services include a common execution engine for code development. Visual Studio .NET and C# The latest release of Microsoft’s Visual Studio .NET provides a language-rich environment for developing a variety of applications. Programming can be done in a variety of lan- guages, such as C, C++, C#, Visual Basic, and more. Applications can range from standal- one console (command-line or DOS mode) applications to Microsoft Windows programs. C#, although certainly one of the newest variations of C to hit the market, is just a component of this much larger package. One of Microsoft’s goals, in this release of the Visual Studio .NET, is to allow seamless solutions to project demands. Solutions to a task can include components from C++, Visual Basic, and C# all rolled into one seamless exe- cutable file. This book concentrates on the C# aspect of this package as it applies to creating Win- dows applications. If you are interested in a more detailed treatment of the C# language, we would recommend another of our books, C# Essentials, published by Prentice Hall (ISBN 1 2 Chapter 1 • A Radically New Approach: C# and Windows 0-13-093285-X), 2002. This is just the book if you are the type of programmer who likes to discover all of nuances of a language. Our first task will be to learn how to use Visual Studio to create a simple C# console application. Then we’ll do a quick study of the most important aspects of the C# language. Finally, we’ll turn our attention to our first C# Windows application and see what is in store for us in the remainder of this book. Building C# Applications C# applications fall within two distinct categories: command-line or console applications and Windows applications. By using the AppWizards, you’ll find that both are easy to cre- ate in terms of the template code necessary to outline a project. For our first project we build the familiar Hello World console application. We’ll name this project HelloWorld. The second application, which appears closer to the end of this chapter, is called CircleArea. This is a full-fledged, object-oriented Windows application. Both projects are intended to introduce you to the Visual Studio AppWizards and show you how to build the basic template code that will be part of every project developed in this book. These are good places to set bookmarks and take notes in the margins. Your First C# Console Application To build a console application using C#, start the Visual Studio. Select the File | New | Project sequence to open the New Project dialog box, as shown in Figure 1–1. Your First C# Console Application 3 Figure 1–1 The New Project dialog box allows us to specify a C# console application. Name this project HelloWorld, and specify a subdirectory under the root directory, as shown in Figure 1–1. When you click the OK button, the C# AppWizard creates the template code shown in Figure 1–2. 4 Chapter 1 • A Radically New Approach: C# and Windows Figure 1–2 The AppWizard’s C# template code for a console application. This template code can now be modified to suite your purposes. Figure 1–3 shows how we altered the template code for our HelloWorld project. Examine Figure 1–3 and compare it with the following complete listing. Note the addition of just one line of code: using System; namespace HelloWorld { /// <summary> /// Summary description for Class1. /// </summary> class Class1 { static void Main(string[] args) { Your First C# Console Application 5 // // TODO: Add code to start application here // Console.WriteLine("Hello C# World!"); } } } Examine Figure 1–3, once again. Notice that the Build menu has been opened and the Rebuild option is about to be selected. Clicking this menu item will build the project. Figure 1–3 The template code is altered for the HelloWorld project. 6 Chapter 1 • A Radically New Approach: C# and Windows When you examine this simple portion of code, you notice many of the elements that you are already familiar with from writing C or C++ console applications. Figure 1–4 shows the Debug menu opened and the Start Without Debugging option about to be selected. Make this selection to run the application within the integrated environment of the Visual Studio. Figure 1–4 Running the program from within Visual Studio. When the program is executed, a console (command-line or DOS) window will appear with the programs output. Figure 1–5 shows the output for this application. Now, let’s briefly examine the familiar elements and the new additions. First, the application uses the System directive. The System namespace, provided by the NGWS at runtime, permits access to the Console class used in the Main method. The use of Con- sole.WriteLine() is actually an abbreviated form of System.Console.WriteLine() where System represents the namespace, Console a class defined within the namespace, and WriteLine() is a static method defined within the Console class. C# Programming Elements 7 Figure 1–5 The console window shows the project’s output. Additional Program Details In C# programs, functions and variables are always contained within class and structure def- initions and are never global. You will probably notice the use of “.” as a separator in compound names. C# uses this separator is place of “::” and “->”. Also, C# does not need forward declarations because the order in not important. The lack of #include statements is an indicator that the C# language handles dependencies symbolically. Another feature of C# is automatic memory manage- ment, which frees developers from dealing with this complicated problem. C# Programming Elements In the following sections we examine key elements of the C# language that we use through- out the book. From time to time, additional C# information is introduced, but the material in the following sections is used repeatedly. 8 Chapter 1 • A Radically New Approach: C# and Windows Arrays C# supports the same variety of arrays as C and C++, including both single and multidi- mensional arrays. This type of array is often referred to as a rectangular array, as opposed to a jagged array. To declare a single-dimension integer array named myarray, the following C# syntax can be used: int[] myarray = new int[12]; The array can then be initialized with 12 values using a for loop in the following man- ner: for (int i = 0; i < myarray.Length; i++) myarray[i] = 2 * i; The contents of the array can be written to the screen with a for loop and WriteLine() statement. for (int i = 0; i < myarray.Length; i++) Console.WriteLine("myarray[{0}] = {1}", i, myarray[i]); Note that i values will be substituted for the {0} and myarray[ ] values for {1} in the argument list provided with the WriteLine() statement. Other array dimensions can follow the same pattern. For example, the syntax used for creating a two-dimensional array can take this form: int[,] my2array = new int[12, 2]; The array can then be initialized with values using two for loops in the following manner: for (int i = 0; i < 12; i++) for (int j = 0; j < 2; j++) my2array[i, j] = 2 * i; The contents of the array can then be displayed on the console with the following syn- tax: for (int i = 0; i < 12; i++) for (int j = 0; j < 2; j++) Console.WriteLine("my2array[{0}, {1}] = {2}", i, j, my2array[i, j]); Three-dimensional arrays can be handled with similar syntax using this form: int[,,] my3array = new int[3, 6, 9]; C# Programming Elements 9 In addition to handling multidimensional rectangular arrays, C# handles jagged arrays. A jagged array can be declared using the following syntax: int[][] jagarray1; int[][][] jagarray2; For example, suppose a jagged array is declared as: int[][] jagarray1 = new int[2][]; jagarray1[0] = new int[] {2, 4}; jagarray1[1] = new int[] {2, 4, 6, 8}; Here jagarray1 represents an array of int. The jagged appearance of the structure gives rise to the array’s type name. The following line of code would print the value 6 to the screen: Console.WriteLine(jagarray1[1][2]); For practice, try to write the code necessary to print each array element to the screen. Attributes, Events, Indexers, Properties, and Versioning Many of the terms in this section are employed when developing applications for Windows. If you have worked with Visual Basic or the Microsoft Foundation Class (MFC) and C++ you are familiar with the terms attributes, events, and properties as they apply to controls. In the following sections, we generalize those definitions even more. Attributes C# attributes allow programmers to identify and program new kinds of declarative informa- tion. For example, public, private and protected are attributes that identify the accessibility of a method. An element’s attribute information can be returned at runtime using the NGWS runt- ime’s reflection support. Events Events are used to allow classes to provide notifications about which clients can provide executable code.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages34 Page
-
File Size-