5.1 Basic class structure


Please attempt to write, compile, and execute HelloWorld.cs in the previous chapter [1] if you haven't already done so.

[1] See Chapter 4: Hello C#! on page 34.

Let's review HelloWorld.cs . Notice that it is structurally very similar to a Java class:

 1: // HelloWorld.cs 2: public class TestClass{ 3:   public static void Main (){ 4:     System.Console.WriteLine("Hello C#! Here I come!"); 5:   } 6: } 

Like Java

  • The Main method, and all other methods , must be inside a class [2] (or a struct “ the struct will be explained in Chapter 26).

    [2] In C/C++, the Main method is defined outside a class as a global method. Like Java, and unlike C/C++, C# does not support global methods or variables. Declarations of methods/ variables must always be contained within a class or struct.

  • The Main method must be declared as static and is the 'entry point', or the first method the runtime will look for when your class executes.

  • Main takes in a string array, which corresponds to the command line arguments. [3] The implementation of Main in the class above does not take in any argument. We can replace line 3 with:

    [3] In C/C++, the first element in the array is the application name (e.g. "Hello" in our example), but in the case of C# (like Java), the first element in the array is the command line argument after the application's name .

     3:   public static void Main (string []args){ 

    so that the program can receive command line arguments. Like Java, there is no need to use args as the string array variable name “ any other valid identifier would do.

Unlike Java

  • The Main method can be declared to take in no parameter instead of a string array. In other words, we can declare Main like this:

     3:   public static void Main(){ 

    instead of

     3:   public static void Main(string []args){ 

    In the former case, command line arguments are simply ignored.

  • The Main method may return an int instead of void . For example,

     3:   public static  int  Main(){ 

    This returned int will be useful if you invoke your program from a DOS script which requires a return value for further decision-making purposes.

  • In C#, the Main method must start with a capital 'M'. You can have a main method (with a small 'm') in your class, but that will not be recognized as the starting point method. In Java, main must start with a small 'm'. Remember that both Java and C# are case sensitive.

5.1.1 Another example

Let's take a look at another C# source file so that we can discuss further similarities and differences.

 1: // Test.cs  2:  3: class TestClass{  4:   public static void Main (){  5:     MyClass m = new MyClass();  6:     System.Console.WriteLine(m.Double(99));  7:   }  8: }  9: class MyClass{ 10:   public int Double(int value){ 11:     return value*2; 12:   } 13: } 
Like Java

You can define as many classes (and interfaces) as you want in a single source file.

Unlike Java
  • There is no requirement for the file name (in this case, Test.cs ) to be identical to any class defined in the source file. You can give your source file any legal name which the operating system accepts. There is no need even to give your source file a .cs extension, though you should do that to avoid confusion.

  • You can have as many public classes/interfaces as you want in each source file. In Java, only up to one public class/interface can be defined in each source file.

  • In addition to that, each of these classes (whether public or not) can have its own Main method.

In such cases however, there is a need to indicate to the compiler which Main method is to be the 'starting point' when the file executes. This is done by running the compiler like this:

 c:\expt>csc Hello.cs  /main:  Class1 

where Class1 is the name of the class containing the Main method which you want to start off with when the assembly runs. The class name specified here is case sensitive as you can have two classes of the same name with different capitalization in the same source file. [4]

[4] Please do not do this even though it is legal. Common software sense will tell you not to have identical identifiers differing only in capitalization within the same scope, unless your objective is to introduce software mayhem and sabotage your co-workers .

If you do not specify which Main method the program is to start with, and there is more than one class in the same source file containing a Main method, the C# compiler will give a compilation error. The following compiler error appeared when I tried to compile a source file containing three C# classes ( Class1 , Class2 and Class3 ) “ each with its own Main method “ without the /main option.

[View full width]
 
[View full width]
c:\expt>csc test.cs test.cs(2,22): error CS0017: Program 'test.exe' has more than one entry point defined: graphics/ccc.gif 'Class1.Main()' test.cs(6,22): error CS0017: Program 'test.exe' has more than one entry point defined: graphics/ccc.gif 'Class2.Main()' test.cs(10,22): error CS0017: Program 'test.exe' has more than one entry point defined: graphics/ccc.gif 'Class3.Main()'

On the other hand, the following will compile correctly and produce test.exe . When test.exe is executed, the Main method of Class2 will run.

 c:\expt>csc test.cs /main:Class2 

One possible purpose of having multiple Main methods in each class in a single source file is for debugging purposes. For the same reason, some Java developers like to write main methods for multiple classes within the same package, even though only one of them is meant to be executed.

A source file must have at least one Main method defined in one of the classes in the source file to act as the entry point if it is to be built into an EXE assembly. A compiler error is given if you try to compile a source file which does not contain a Main method:

 1: // Test.cs 2: class TestClass{ 3: } 

An attempt to compile Test.cs above into an EXE assembly like this:

 c:\expt>csc Test.cs 

results in a compile time error:

 error CS5001: Program 'test.exe' does not have an entry point defined 

You can compile test.cs into a DLL assembly though. Use the /target:library option of the C# compiler:

 c:\expt>csc  /target:library  Test.cs 

This time, compilation succeeds and results in the DLL assembly test.dll .

A source file which is to be built into a DLL assembly (or a .DLL library) does not need a Main() method (since DLLs, like Java classes without a main() method, are not executable).

This is a good point to briefly introduce EXE and DLL assemblies. .NET assemblies can be classified broadly into two groups:

  • EXE assemblies which can be executed. EXE assemblies are somewhat similar to Java classes with a main() method. You compile a C# source file into an EXE assembly like this:

     csc Hello.cs 

    The compiled assembly file will be Hello.exe .

  • DLL assemblies which cannot be executed, but which contain classes or interfaces which an EXE assembly, or other DLL assemblies, may use. DLL assemblies can be thought of as Java classes without a main() method. You compile a C# source file into a DLL assembly like this:

     csc /target:library Hello.cs 

    The compiled assembly file will be Hello.dll .

Let's look at a third basic C# class which introduces the C# keyword using .

 1: // HelloWorld2.cs 2:  using System  ; 3: 4: public class TestClass{ 5:   public static void Main (){ 6:  Console.WriteLine  ("Hello C#! Here I come!"); 7:   } 8: } 

HelloWorld2.cs is identical to HelloWorld.cs except for the inclusion of line 2 and a slight alteration to line 6, the statement used to print the Hello World line.

You can use the using keyword to 'import' the System namespace (see section 5.3), so that you can refer to System.Console.WriteLine() as Console.WriteLine() in your codes. Console is one of the core classes within the System namespace, and WriteLine() is a static method of the System. Console class.

Doesn't the using keyword sound like Java's import ? There are some differences though, as you will see later (section 5.3).



From Java to C#. A Developers Guide
From Java to C#: A Developers Guide
ISBN: 0321136225
EAN: 2147483647
Year: 2003
Pages: 221
Authors: Heng Ngee Mok

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