C Sharp

Code Walk-Through

Now that we've established that you can write and execute a C# application, let's walk through the code and look at the basic structure of a C# application.

One-Stop Programming

As you've seen throughout the first couple of chapters and in the "Hello, World" application, my examples have shown the methods of each class being defined within the class definition itself. This is not merely an exercise in convenience on my part, as C++ programmers might think. When programming in C++, you have two options: you can program the implementation of a class's member function directly into the class declaration-an example of inline programming-or you can separate the class declaration and its member function definitions into separate files. In C#, you do not have this choice.

When you define a class in C#, you must define all methods inline-header files do not exist. Why is this a good thing? It enables the writers of classes to create highly mobile code, which is one of the key concepts of the .NET environment. This way when you write a C# class, you end up with a fully encapsulated bundle of functionality that you can easily drop into any other development environment without worrying how that language processes include files or if it has a mechanism for including files within files. Using this "one-stop programming" approach, you can, for instance, take an entire class and drop it into an Active Server Pages (ASP) page and it will function just as it would if it were being compiled into a desktop Windows application! -

Classes and Members

First you see in a basic C# application the name of a class or namespace. As you learned in Chapter 1, "Fundamentals of Object-Oriented Programming," you should select a class name that describes a problem domain-for example, Invoice, PurchaseOrder, or Customer. The beginning and ending of a class's definition is marked by the "curlies": { and }, respectively. Everything in between is considered part of that C# class. Notice above that we have a class named HelloWorld and everything in the application is defined within the context of that class.-

All of a class's members are defined within the class's curlies. These include methods, fields, properties, indexers, attributes, and interfaces. I'll cover the specific details of how to define these different C# elements in the next several chapters.

The Main Method

Every C# application must have a method named Main defined in one of its classes. It doesn't matter which class contains the method-you can have as many classes as you want in a given application-as long as one class has a method named Main. In addition, this method must be defined as public and static. The public keyword is an access modifierthat tells the C# compiler that anyone can call this method. As you saw in Chapter 1, the static keyword tells the compiler that the Main method is a global method and that the class does not need to be instantiated for the method to be called. This makes sense once you think about it because otherwise the compiler would not know how or when to instantiate your class. Because the method is static, the compiler stores the address of the method as the entry point so that the .NET runtime knows where to begin the execution of your application.

NOTE
The demos in this chapter show the Main method as returning void and not receiving any arguments. However, you can define your Main method as returning a value as well as taking an array of arguments. These options, as well as how to iterate through the passed arguments to an application's Main method, are covered in Chapter 5, "Classes."

by BrainBellupdated
Advertisement: