Building a Program with C#

One of your first tasks in learning C# is to create a C# program. Listing A.1 shows a simple C# program that simply prints a message to the Console.

Listing A.1. Simple C# Program
using System;
namespace Hello
{
    class HelloMain
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello!");
        }
    }
}

First, notice that a namepace encloses the entire program. This is strictly not required. If the namespace is taken out, leaving the class HelloMain, the program would still compile and run. However, it is a good idea to get in the habit of enclosing your programs with a namespace. This prevents conflicts with other modules and libraries.

Classes can only exist directly in a namespace. You cannot put methods or fields directly under a namespace. A namespace only specifies an extra qualifying “name” for the classes contained in the namespace. Look, for example, at Listing A.2.

Listing A.2. Namespace Example
namespace Hello
{
    class Write
    {
        static void OutputLine(string line)
        {
            System.Console.WriteLine(line);
        }
    }
}

The long form of the method OutputLine would be Hello.Write.OutputLine. If you were to include using Hello;, you could shorten the call to the method to Write.OutputLine. You cannot include a class in a using statement; it is just to shorten namespace specifications.

After the namespace is the class definition. In Listing A.1, notice that only one method is in the class, and it is not only static, but it is the entry point for the program. When the CLR loads a C# program, it looks for a static Main method, and that is where the program begins execution. In the previous paragraph explaining Listing A.2, you saw how any static method could be called. You call static method with class type.method. (In Listing A.2, the class type is Hello.Write and the method is OutputLine.)

The only task that the Main method performs is to write a message out to the Console. Because using System; was used in Listing A.1, you don't have to specify System.Console.WriteLine—just Console.WriteLine. The only time that this could be confusing is if you wanted to look up documentation on this method or the Console class; you would need to know that the Console class is part of the System namespace.

To compile and run this program, you just need to run the C# compiler like this:

csc hello.cs

Note

A complete description of the command-line options for the C# compiler is part of the SDK, “C# Compiler Options,” at ms-help://MS.VSCC/MS.MSDNVS/cscomp/ html/vcrefCompilerOptions.htm.


Now add a method to this class so that you can modify the program, as in Listing A.3.

Listing A.3. Adding a Simple Method to the Hello Program
using System;
namespace Hello
{
    class HelloMain
    {
        static void Output(string message)
        {
            Console.WriteLine(message);
        }
        static void Main(string[] args)
        {
            Output("Hello!");
        }
    }
}

Now another function or method exists, and it is also static. Static methods can only call static methods and reference static fields in the class. Non-static methods and fields will be covered in more detail when C# objects are discussed.

Now you can organize the “output” functions into a library. Listing A.4 shows the output function in a class by itself.

Listing A.4. A "Library" of Functions
using System;
namespace Hello
{
    public class Output
    {
        public static void OutputLine(string message)
        {
            Console.WriteLine(message);
        }
    }
}

Listing A.5 shows how to call a method into this library.

Listing A.5. Calling a Function into a Library
namespace Testing
{
    class HelloMain
    {
        static void Main(string[] args)
        {
            Output.OutputLine("Hello!");
        }
    }
}

Figure A.1 shows two different ways to compile and link this program with multiple modules.

Figure A.1. Two different methods to compile and link a multifile program.


The first method shown in Figure A.1 shows a two-step process. First, you create the library (in this case, it is called output.dll), and next, you compile and link in that library. You would use this method to distribute or use a library. Assume that output.dll was your library. You could build the library as in the first step, distribute this library to your customers, and instruct them to link in your library using the second step. On the other hand, if you are a consumer and you want to use the library, you would link in the library that you obtained from a third party using the second step. The second method requires that you have source for both the caller and the callee modules. You would use this method if you were only interested in organizing your code into functional modules and you were not interested in distributing this code as a library.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.189.177