LJ Archive

Brief Introduction to C#

The C# language was designed as a high-level object-oriented language that complements the Common Language Infrastructure. At the head of the design team were Anders Hejlsberg (of Turbo Pascal and Delphi fame), Scott Wiltamuth and Peter Golde.

At first glance, C# looks a lot like Java. Though syntactically similar, C# adds several features not present in Java. Some of the features are borrowed directly from C++, such as operator overloading, user-defined conversions, true rectangular arrays and pass-by-reference. Other features, like classes, interfaces, structs, enums and the more exotic delegates and function pointers, come from a mixture of C, C++ and Java.

Although supporting the key concepts of a modern object-oriented language, C# also has built-in support for component constructs such as properties, methods, attributes, events and documentation. These greatly simplify the work done by developers when writing components and more easily integrate into RAD environments.

Programmers comfortable with Java will have no trouble getting through this introduction to C#. Due to space limitations, the concepts of interest to seasoned object-oriented programmers cannot be addressed. However, the Resources section contains links to discussions of the more-advanced C# language topics.

To begin our introduction to C#, let's look at the venerable “hello world” program:

/*
   Code listing for hello.cs
*/

using System;

public class HelloWorld
{
   public static void Main()
   {
      for (int x=0; x<3; x++){
         // Console from the System namespace
         Console.WriteLine("Hello World");
      }
   }
}

All programs must be constructed of classes.

At the top of the program, you will notice a comment block. C# uses the same comment styles as Java and C++. Multiline comments use an opening /* with a closing */. For comments that span only one line, the double slash (//) method is also acceptable.

Beneath the comment, there are no header files as in C and C++. There is, however, a statement that looks similar to import in Java. The “using” keyword in C# is employed to declare a namespace, which is basically a collection of classes. By declaring the namespace, it is not necessary to use the full formal name when calling a library class. In this case, System is the class library namespace we are declaring. C# is a case-sensitive language, so “System” is different from “system”. Also, like Java, all statements end with a semicolon.

The next line down begins the outermost class. C# allows for multiple classes per file. As a result, the filename does not need to match the name of the outermost class as it does in Java. The filename, however, must end with the .cs extension.

The next line after the opening curly bracket of the HelloWorld class is the Main() method. Every program must have a Main() method. It acts as the entry point for the program. Unlike C++ and Java, Main() is capitalized. In the declaration for the main method you also see public static void. Public sets the visibility of the method and who may access it (anyone basically). The static keyword allows the method to be entered without first creating an object of type HelloWorld. Finally, the void keyword declares that the method returns no value. In C#, Main() methods can return no values (void) or an int.

Inside the Main() method is the following loop:

for (int x=0; x<3; x++){
   // Console from the System namespace
   Console.WriteLine("Hello World");
}

The format of the for loop should look familiar to any C, C++ or Java programmer. The first portion of the for loop, int x=0, declares an integer named x and sets it equal to 0. This variable will be used as the loop counter. The middle portion of the loop, x<3, is the logical test that is used to determine when the loop should end. Here, when the value of x is greater than 3, the test will fail and the loop will exit. The last part of the for loop is the update section. Similar to C++, C# allows for post incrementing and decrementing. The statement x++ is shorthand for x=x+1. After each loop iteration, the update portion is executed. In the example, the loop will iterate three times.

C# has support for while loops, do/while loops, for loops and foreach loops.

Inside the for loop is a call to the Console.WriteLine() method. The formal name of this method is System.Console.WriteLine(), but because we declared we were using the System namespace, we can leave off the System part. In long programs this can save a great deal of typing, but if used recklessly, it may become difficult to understand which namespace is being used. The Console.WriteLine() method outputs text to the system console.

The curly braces at the end of the example code close out the loop, method and class.

The code listing can be saved as a text file named hello.cs and compiled with the Mono C# compiler mcs. Running the resulting output file, hello.exe, produces the following results:

[jdq@newton mono]$ mcs hello.cs
Compilation succeeded
[jdq@newton mono]$ mono hello.exe
Hello World
Hello World
Hello World

Of course, there is a lot more to C# than what can be described in this introduction. Some good C# tutorials are listed in the Resources section.

LJ Archive