Lesson07 PDF
Lesson07 PDF
Lesson07 PDF
B E G I N N E R S ’ S
C# TUTORIAL
7 . I N T R O D U C T I O N T O C L A S S E S
WWW.CSHARP–STATION.COM
PAGE 1 OF 5
COPYRIGHT © 2000–2003 C# STATION , ALL RIGHTS RESERVED
BEGINNERS C# TUTORIAL
LESSON 7 - INTRODUCTION TO CLASSES
• Implement Constructors.
• Know the difference between instance and static members.
• Understand 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. Every class has a
constructor, which is called automatically any time an instance of a class is
created. The purpose of constructors is to initialize class members when
the class is created. Constructors do not have return values and always
have the same name as the class. Listing 7-1 is an example of a class.
// 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
}
}
PAGE 2 OF 5
COPYRIGHT © 2000–2003 C# STATION , ALL RIGHTS RESERVED
BEGINNERS C# TUTORIAL
LESSON 7 - INTRODUCTION TO CLASSES
// Instance of OutputClass
OutputClass outCl = new OutputClass("This is printed by the output class.");
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. 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.
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:
PAGE 3 OF 5
COPYRIGHT © 2000–2003 C# STATION , ALL RIGHTS RESERVED
BEGINNERS C# TUTORIAL
LESSON 7 - INTRODUCTION TO CLASSES
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.
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
PAGE 4 OF 5
COPYRIGHT © 2000–2003 C# STATION , ALL RIGHTS RESERVED
BEGINNERS C# TUTORIAL
LESSON 7 - INTRODUCTION TO CLASSES
• Nested Classes
Those items not covered in this lesson will be covered in later lessons.
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.
PAGE 5 OF 5
COPYRIGHT © 2000–2003 C# STATION , ALL RIGHTS RESERVED