Creating C# Programs
If you are using one of the Microsoft Visual products to develop your programs (Visual Studio .NET or Visual C# .NET), you have a complete program editing environment, including help files, graphical wizards, and command completion wizards. If you are using the .NET Framework SDK package, you are on your own for producing and compiling your C# code. Although this SDK’s features pale in comparison to the fancy Visual packages, it is nonetheless just as valid a way to produce C# applications.
The first step to working with C# programs in the .NET Framework development environment is to associate the C# source code filename extension with a text editor. This will make editing programs much easier; you can just double-click a program from within the Windows Explorer program to begin editing. The type of editor you select is important. Choose one that allows you to save your source code files in text mode rather than a Microsoft Word .doc file or other word processing document, because the C# compiler must be able to interpret each line of code. If you do select a word processing package to edit your C# programs, make sure that you save all of the files in text format.
After you select an editor, associate the .CS file type to the editor application within the Windows Explorer: right-click a C# program, select the Open With option, and select the appropriate application from the list.
If you are new to C#, you may want to practice compiling and debugging C# programs. To do that, you must first have a sample program to work with. Listing 1.1 shows a simple program that demonstrates some basic C# programming principles.
Listing 1.1: SampleClass.cs program
class DataClass
{
private int a;
private int b;
public DataClass(int x, int y)
{
a = x;
b = y;
}
public int addem()
{
return a + b;
}
}
class SampleClass
{
static int sampleX;
static int sampleY;
public SampleClass()
{
DataClass sample = new DataClass(sampleX, sampleY);
System.Console.WriteLine("The result is: {0}", sample.addem());
}
public static void Main(string[] argv)
{
if (argv.Length != 2)
{
System.Console.WriteLine(" Usage: SampleClass x y");
return;
}
sampleX = System.Convert.ToInt16(argv[0]);
sampleY = System.Convert.ToInt16(argv[1]);
SampleClass starthere = new SampleClass();
}
}--------------------------------------------------------------------------
The sample program contains two separate C# classes, DataClass and SampleClass. DataClass declares two private integers (that is, they areonly accessible from the DataClass class), aconstructor for the class, and a method that manipulates the data. The DataClass constructor defines what happens when DataClass is instantiated from a program:public DataClass(int x, int y){ a = x; b = y;}The default constructor requires two integers. The two integersare assigned to the internal private variables a and bdefined in the class. The one method that is defined, addem, returns an integer value that is the addition of thetwo private variables:public int addem(){return a + b; }
| Note | Once DataClass is defined in the program, other classes in the program can use it. In C#, unlike C++, you can use classes before they are defined without first declaring them. The SampleClass code could just as easily have been defined first, before the DataClass definition. The C# compiler will realize that the required class is located later in the program. You can even declare classes in separate files, as long as you include them on the command line when compiling. The compiler will only complain if declared classes are never found in any of the program files listed on the command line. |
SampleClass contains two static integer
variables, a constructor, and a Main() method, which
instructs the C# compiler where to start execution of the program. The Main() method first checks to ensure that two command-line
parameters have been entered, converts them to integer values, and assigns them
to the two integer variables defined. It then creates an instance of SampleClass using the statement
SampleClass starthere = new SampleClass();
This forces the CLR to execute the SampleClass constructor to create a new instance of the
The SampleClass constructor code creates
an instance of DataClass, passing the two integers to
the DataClass class constructor. The addem() method is called from the instantiated SampleClass variable and returns the result of the addition
method. The following line is used to display the result of the addem() method to the console screen:
System.Console.WriteLine("The result is: {0}", sample.addem());The symbol {0} is used as a placement
value to represent a variable listed after the text string, in this case
replaced with the return value of the sample.addem()
method. You can add additional variables by continuing the placement numbers
({1}, {2}, and so on). Each additional variable is added to the variable list
separated by commas.
After typing the program code, you must save the file using a
.CS extension, which identifies the file as a C# code
file. Once you save the file, you are ready to compile it.

No comments:
Post a Comment