This lesson introduces you to C# Classes. Our objectives are as
follows:
- Implement Constructors.
- Know the difference between instance and static members.
- Using Destructors.
- Familiarization with Class Members.
Since the beginning of this tutorial, you have been using classes. By
now, you should have a sense of what a class is for and how to specify
one. This lesson will build upon what you already know and introduce the
various class members.
Classes are declared by using the keyword "class" followed by the
class name and a set of class members surrounded by curly braces.
Constructors do not have return values and always have the same name as the
class. Listing 7-1 is an example of a class.
Listing 7-1. 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();
}
}
Listing 7-1 shows two classes. The top class, "OutputClass",
has a constructor, instance method, and a destructor. It also had a field
named "myString". Constructors are used to initialize a class'
data members. In this case, the OutputClass constructor accepts a string
argument. This string is copied to the class field myString.
Constructor's 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. However, a
constructor with no arguments is not always useful. To make default
constructors more useful, you can implement them with initializers. Here
is an example:
public OutputClass() :
this("Default Constructor String") { }
Imagine this constructor was included in class OutputClass from Listing
7-1. This default constructor is followed by an initializer. The
colon, ":", marks the beginning of the initializer, followed by the
"this" keyword. The "this" keyword refers to this
particular object. It effectively makes a call to the constructor of the
same object it is defined in. After the "this" keyword is a
parameter list with a string. The action taken by the initializer above is
to invoke the OutputClass constructor that takes a string type as an
argument. The initializer helps you to ensure your class fields are
initialized when a class is instantiated.
The example above illustrates how a class can have multiple
constructors. The specific constructor called depends on the number of
parameters and the type of each parameter.
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 <classname>.<static
class member>. The instance names are "oc1" and
"oc2".
Suppose OutputClass had the following static method:
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. There is only ever one copy of a static class member.
Another type of constructor is the static constructor. 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.
Destructors are places where you could put code to release any resources your
class was holding during it's lifetime. They are normally called when the
C# garbage collector decides to clean your object from memory.
So far, the only class members you've seen are Fields, Methods, Constructors,
and Destructors. Here is a complete list of the types of members you can
have in your classes:
- Constructors
- Destructors
- Fields
- Methods
- Properties
- Indexers
- Delegates
- Events
- Nested Classes
Those items not covered in this lesson will be covered in later lessons.
In summary, you can declare normal 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.
C# : Class Inheritance