Create instance for what ?

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.