1.1 Understanding Visual C .NET Program Layout

 <  Day Day Up  >  

1.1 Understanding Visual C# .NET Program Layout

You want to create a simple Visual C# .NET console application and inspect the generated code.


Technique

In the Visual Studio .NET IDE, click on File, New, Project (Ctrl+Shift+N) and select Visual C# Projects from the list of project types. In the list of templates, select the Console Application template and type in a name for the project.

Comments

For most of this chapter and in several projects throughout this book, you'll find yourself working with console-based applications. Their minimalist nature allows you to concentrate on the topic being discussed without having to traverse through extraneous, potentially distracting code.

Even though it is just a simple console-based application that currently provides no functionality, it does serve as a good illustration for some of the various pieces that make a C# application. Once you create your project, the Visual Studio .NET IDE opens the application's main program file, which should look similar to Listing 1.1.

Listing 1.1 A Simple Visual C# .NET Console Application
 using System; namespace _1_ProgramLayout {     /// <summary>     /// Summary description for Class1.     /// </summary>     class DateTimePrinter     {         /// <summary>         /// The main entry point for the application.         /// </summary>         [STAThread]         static void Main(string[] args)         {             Console.WriteLine( "Today is {0}", DateTime.Now );         }     } } 

The first couple of lines relate to an organizational construct known as the namespace. Namespaces convey a sense of relationships between like objects. For instance, if you have some objects with such names as Frame, Engine, Wheel, Light , and Seat , you immediately see the relationship these objects possess in that they all belong on an automobile. In this example, the namespace could be Automobile .

On the first line, the program is letting the compiler know that it wants to use some of the objects within the System namespace. Therefore, anytime you want to use one of these objects, you do not need to preface the object name with the namespace name. For instance, the WriteLine statement prints out a small message in the console. The Console object is a class within the System namespace. Without the using directive, you have to qualify each type you want to use with the namespace it is declared in. In this instance, the line in Listing 1.1 would be

 
 System.Console.WriteLine( "Today is {0}", DateTime.Now ); 

In the listing, you can see a namespace declaration, which is simply the name you gave the project when you initially created it. Although the wizard placed an initial type in a namespace for you to use, the namespace declaration itself is purely optional. However, we recommend that you do get into the habit of using namespaces to enhance code organization and readability.

Within the automatically generated namespace is a class called Class1 , which was renamed to DateTimePrinter to more accurately describe the type's functionality (albeit somewhat limited). A class is best described as a data type that can contain various associated methods to interact with it. Whenever an instance of that class is created or instantiated , it becomes a usable object to which you can get or set various properties, receive notification of certain events, and tell it to perform some action using one of its member functions. Chapter 2, "Objects and Components," delves deeper into the many ways classes are used within C# and the .NET Framework.

The last important component of our simple console application is the application's entry point, denoted by the static member function Main . Each application must contain an entry point so that the Common Language Runtime (CLR) can effectively start the application by handing control over to you. Once the Main function exits, the application ends. Just before the Main method declaration is the STAThread attribute. This attribute denotes the threading model your application uses. However, it is only applicable if you plan to use COM Interop, discussed in Chapter 24, "COM Interop," and is ignored if you do not.

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net