Search This Blog

2009-12-01

Tutorial 6:Introduction to Classes

Example C# Classes: Classes.cs
// Namespace Declaration
using System;

// helper class
class OutputClass
{
string myString;

// Constructor
public OutputClass(string inputString)
{
myString = inputString;
}

// Instance Method
public void printString()
{
Console.WriteLine("{0}", myString);
}

// Destructor
~OutputClass()
{
// Some resource cleanup routines
}
}

// Program start class
class ExampleClass
{
// Main begins program execution.
public static void Main()
{
// Instance of OutputClass
OutputClass outCl = new OutputClass("This is printed by the output class.");

// Call Output class' method
outCl.printString();
}
}
The top class, OutputClass, has a constructor, instance method, and a destructor. It also had a field named myString. Notice how the OutputClass constructor is used to initialize data members of the class. In this case, the OutputClass constructor accepts a string argument, inputString. This string is copied to the class field myString.
Constructors are not mandatory, as indicated by the implementation of ExampleClass. In this case, a default constructor is provided. A default constructor is simply a constructor with no arguments.
In C#, there are two types of class members, instance and static. Instance class members belong to a specific occurrence of a class. Every time you declare an object of a certain class, you create a new instance of that class. The ExampleClass Main() method creates an instance of the OutputClass named outCl. You can create multiple instances of OutputClass with different names. Each of these instances are separate and stand alone. For example, if you create two OutputClass instances as follows:
OutputClass oc1 = new OutputClass("OutputClass1");
OutputClass oc2 = new OutputClass("OutputClass2");
You create two separate instances of OutputClass with separate myString fields and separate printString() methods. On the other hand, if a class member is static, you can access it simply by using the syntax .. The instance names are oc1 and oc2.
Suppose OutputClass had the following static method:
public static void staticPrinter()
{
Console.WriteLine("There is only one of me.");
}
Then you could call that function from Main() like this:
OutputClass.staticPrinter();
You must call static class members through their class name and not their instance name. This means that you don't need to instantiate a class to use its static members. There is only ever one copy of a static class member. A good use of static members is when there is a function to be performed and no intermediate state is required, such as math calculations. Matter of fact, the .NET Frameworks Base Class Library includes a Math class that makes extensive use of static members.
Another type of constructor is the static constructor. Use static constructor to initialize static fields in a class. You declare a static constructor by using the keyword static just in front of the constructor name. A static constructor is called before an instance of a class is created, before a static member is called, and before the static constructor of a derived class (covered in a later chapter). They are called only once.
OutputClass also has a destructor. Destructors look just like constructors, except they have a tilde, "~", in front of them. They don't take any parameters and do not return a value. Destructors are places where you could put code to release any resources your class was holding during its lifetime. They are normally called when the C# garbage collector decides to clean your object from memory.
In summary, you can declare instance and static constructors. You know how to initialize class fields. When there is no need to instantiate an object, you can create static class members. You can also declare destructors for cleaning up resources.

No comments: