FREE BOOK

Chapter 1: Introduction to C#

Posted by Apress Free Book | C# Language December 08, 2008
This Chapter will introduce you to C#. You will learn how to write and compile C# programs, C# syntaxes, data types, control flow, classes and their members, inter-faces, arrays, and exception handling I'll begin with an overview of the language.

Exception handling
 
Exception handling in C# is pretty much like exception handling in C++, in which you use a try. . . catch block. If you don't catch an exception in C#, it will display it as a nasty message box to the user and then terminate the application. C# also adds the keyword finally. Exception handling works in this way: The try block executes the code within it, and if an exception occurs anywhere within that block, the code jumps directly to the catch block. If the exception in the catch block matches the exception thrown, the catch block is executed and the program continues. The finally block is similar to a catch block, except the code in the finally block is executed regardless of whether an exception is thrown. There are some built-in exception types in C#.

Table 1-11. Exception types in C#

EXCEPTION NAME DESCRIPITION
Exception Base class for all exceptions. If you want to catch any exception, use this.
SystemException Base class for all exceptions generated at run-time. This will also catch most exceptions.
NullReferenceException An exception thrown when you attempt to use a null reference.
IndexOutOfRangeException Exception thrown when you attempt to use an array element that is not within the constraints of the array bounds.


Listing 1-38 is an example of a NullReference exception.

Listing 1-38. a null reference exception

public void ExceptionTest1()
{
try
{
string mystring = null;
mystring.Insert(0, "hello");
}
catch(Exception ex)
{
Console.WriteLine("Exception - {0}");
ex.Message.ToString());
}
}

Listing 1-39 is an example of an IndexOutOfRangeException.

Listing 1-39. An IndexOutOfRange exception

public void ExceptionTest2()
{
try
{
char[] MyChartList = new char[2]{'a', 'b'};
// Remember last array index is 1
char mychar = MyCharList[2];
}
catch(Exception ex)
{
Console.Write.Line("Exception - {0}",
ex.Massage.ToString());
}
}

Listing 1-40 is an exception- handling example

          public static void Main()
          {
                   int aValue = 5;
                   try
                   {
                             int [] MySequence = new int[]{3,5,6};
                             aValue = MySequence[4];
                   }
                   catch(Exception e)
                   {
System.Console.WriteLine("Caught the exception - -{0}", e.Massage.ToString());
                   }
                   finally
                   {
                             // Do any clean up
                             // . . . . . . . . . . . . . .
                             System.Console.WriteLine(" The value = {0}", aValue);
                             System.Console.ReadLine();
                   }
          }
}

}

The Exception object catches all exceptions. The following code catches the overflow exception with the following output to the console:

Caught the Exception - An exception of type System.IndexOutOfRangeException was thrown.
The value = 5

Documenting your source code

Another good feature of C# language is its programmatic support of source-code documentation. The source- code documentation is a good programming practice of software development lifecycle for long projects. C# language supports programmatic documentation with the help of XML files. You store documentation in an XML file, tell the compiler to look for the file and the compiler puts the documentation for you in the source code. The comments in the source code start with three slashes (/ / /) instead of two (/ /).

You can even set documentation using the VS.NETIDE. You go to project's properties page by right- clicking on the project from Solution Explorer, going to build Properties, and setting the XML file as XML Documentation File option.

MSDN documentation has a nice tutorial on XML documentation file under visual studio .NET/visual basic and visual C#/C# Programmer's Reference/C# Language Features/XML Documentation.

Summary

This article offered an overview of the new Microsoft language, C#. C# takes the best features of many present -day programming languages. You became familiar with some of the language's syntax. You learned how to write and compile your first command-line program with the "Hello, C# World!" example. You also became familiar with classes and their members, their scopes, and how to use them. You learned about some unique features, such as events the indexers, which were not available in languages such as C++. You also looked at some useful objects in C#, such as arrays and exceptions.

Total Pages : 10 678910

comments