Please attempt to write, compile, and execute HelloWorld.cs in the previous chapter [1] if you haven't already done so.
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
Unlike Java
5.1.1 Another exampleLet'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 JavaYou can define as many classes (and interfaces) as you want in a single source file. Unlike Java
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]
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]
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:
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). |