The SampleClass.cs example program defined two separate classes in one source code file. This was easy to do for a small example, but larger programs can get confusing when you’re combining classes into one source code file. Often it is best to create a separate source code file for each class used in the application. This allows better code management, especially when several people are working on an application that contains hundreds of classes. For example, two separate files could be created:
- •DataClass.cs for the DataClass class code
There are a few things to be careful of when you separate classes out into discrete source code files. First, you must ensure that the C# compiler can find them at compile time. The easiest way to do this is to include all related source code files on the command line, as follows:
C:\>csc SampleClass2.cs DataClass.cs
Be careful when you do this, however, because the source code file listed first will be the default .exe filename. If you want to change the .exe filename, you can use the /out: command line switch:
C:\>csc /out:SampleClass2.exe DataClass.cs SampleClass2.cs
Another issue is the importance of telling the compiler where the program execution starts. If only one class has a Main() section defined, this will work fine. However, sometimes different classes can use methods from other classes, but both classes may contain a Main() method. This would confuse the compiler, as it would not know from which Main() method to start to run the program.
A command-line switch for the csc.exe program solves this problem. The /main:switch defines the class that contains the Main() method you want to use:
C:\>csc /main:SampleClass SampleClass2.cs DataClass.cs
Notice that you must specify the class that the Main() method is in, not the source code filename.

No comments:
Post a Comment