Wednesday, March 19, 2008

Static and non-static

Hi everybody, how are you doing?
Here we discuss some theoretical stuff for the mid-term exam. Sorry guys, I know we all know those stuff, consider it a quick revision. I hope I cover every thing. If I forgot to mention anything or said anything incorrect, please correct me. I welcome your comments.

The this keyword
We can say that this refers to the object of the class which will execute the code.
The equivalent keyword of this in Visual Basic.Net is Me which is a very self expressing name.
You cannot assign a value to this. A line like this = null; or this = new Foo(); causes compilation error because this is read-only.
The this keyword is also used to create indexers. It is also used in C# 3.0 to create extension methods.

The static keyword
Static members (variables or methods) don’t belong to a particular object. They belong to the class.
You don’t need to create an object to access a static member, because it belongs to the class.
The equivalent key word of static in Visual Basic.Net is Shared. This is a self expressing name because static variables are shared across all instances (objects) of the class.
The static keyword can be combined with other keywords (private, public …) in a meaningful context.
Static members can address only other static members of the same class but not the non-static ones.
Non-static members can access static members of the class.

Static variables
Consider the following class.

class Foo
{
    public static int SharedVariable;

    public void AddOneToTheSharedVairable() // this is an instance function. can only be called by an object
    {
        SharedVariable += 1; // static member are accessible
    }
}

Take a look at the following code snippet.

Foo.SharedVariable = 5; // I don’t have to created any objects of the class before I access a static member. note that the variable is also public

Foo f1 = new Foo();
// f1.SharedVariable // error. the static member doesn’t belong to the object but it belongs to the class.
Foo f2 = new Foo();
// now we have created 2 objects of the Foo class

f1.AddOneToTheSharedVariable(); // the static variable is changed by an object of the class
// now lets see what happened to the static variable
Console.WriteLine(Foo.SharedVariable); // 6

f2.AddOneToTheSharedVariable(); // now the object f2 is also changing the static variable
Console.WriteLine(Foo.SharedVariable); // 7

Static methods and the this keywords
Static functions and static properties can’t address the this keyword because they belong to the class not to an object. The this keyword is not available in the context of static members.

Notes:
• When all objects should share the same value of a variable you should use static variables. Example, a war game, each soldier object should know how many soldiers are there in the war field in order to determine how will he attack they enemy. So, instead of having a non-static variable for each soldier object, use a static variable to hold the count of the soldiers, this way saves memory and processing.
• When you want a function to be called without creating an object first make it static.
• If you have the choice between static and non-static function go for the static function, although it depends on the situation, this saves you the time of creating objects.
• Remember, static members cannot access non-static members.
• The static keyword cannot be used with constants because constants are static by nature.
• A static member cannot be marked as override, virtual, or abstract.
• The static keyword is used in operator overloading and when defining implicit and explicit type casting. You don’t create an object to perform the ‘+’ operator.

Static classes
A static class means that all its members are static and must be static. A static class cannot have non-static members.
We all are familiar with the System.Math class. This static class has static functions (Sin, Pow…)
A static class is sealed by default i.e. cannot be inherited.
You cannot create objects of a static class.

Notes:
• Usually, when you want to create a utility class like the System.Math you would use the static keyword.

For further information about C# keywords, see this CodeProject article by Marc Clifton.

1 comment:

Anonymous said...

Thanks man, good work, ;)