Hello friends !
This time, we will talk about a subject that I have ever difficulty to learn, because I didn’t urderstand very well for what the instance is used.
Classes are declared by using the keywordย classย followed by theย classย name and a set ofย classย members surrounded by curly braces. Everyclassย has a constructor, which is called automatically any time an instance of aย classย is created. The purpose of constructors is to initializeย classmembers when an instance of theย classย is created. Constructors do not have return values and always have the same name as theย class.ย
Example:
// 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();
}
}
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 theOutputClassย 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 aclassย 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:
ย ย ย 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.
Do you understandย ? So, let’s practice !
Font:ย http://www.csharp-station.com/Tutorials/Lesson07.aspx