I have some experience in developing with Java, Javascript and PHP. I am reading
ID: 643350 • Letter: I
Question
I have some experience in developing with Java, Javascript and PHP.
I am reading Microsoft Visual C# 2010 Step by Step which I feel it is a very good book on introducing you to the C# language.
I seem to be having problems in understanding the static keyword. From what I understand this far if a class is declared static all methods and variable have to be static. The main method always is a static method so in the class that the main method exists all variables and methods are declared static if you have to call them in the main method. Also I have noticed that in order to call a static method from another class you do not need to create an object of that you can use the class name.
What are the advantages of declaring static variables and methods?
When should I declare static variable and methods?
Explanation / Answer
The static keyword in C# is refering to something in the class, or the class itself, that is shared amongst all instances of the class. For example, a field that is marked as static can be accessed from all instances of that class through the class name.
public class SomeObject
{
//Static Field
static int Foo = 3;
//instance field
private int _Foo2 = 4;
//instance property
public int Foo2{get{return _Foo2;}set{_Foo2 = value;}}
//static constructor
public static SomeObject CreateSomeObject(int fooValue)
{
SomeObject retVal = new SomeObject();
retVal.Foo2 = fooValue;
return retVal;
}
//Parameterless instance constructor
public SomeObject()
{
}
public static int Add(int x)
{
//Static methods can only deal with local variables, or fields that
// are also static in the class. This one adds x to the static member foo
return x + Foo;
//Foo2 is not accessable here!
}
//Instance method
public void AddSomething(int x)
{
//Add x to the property value of Foo2
return x + this.Foo2;
//Note that Foo *is* accessable here as 'SomeObject.Foo'
}
}
I can honestly say that I have never used a class marked as static with the exception of creating extention methods (Quick tutorial on extension methods).
Anyways, there are specific design patterns for utilizing static methods, such as factory pattern and singleton pattern, but the important thing to remember is that static methods and constructors do not deal with any specific instance of a class (unless you pass one in). So utilizing static methods is more of a stateless implementation of a methods, e.g. to do calculations or to do a comparison between objects. The "Main" method you are refering to is always static, but to see it from a different point of view, see this article.
To follow up with this, here is how the difference between static and instantiated methods, fields and properties are called.
public static void Main(string[] args)
{
//This is a static method that starts a thread in an application
// space. At this point not everything actually has to be static...
//Here is an instantiation with a parameterless contruction
SomeObject obj = new SomeObject();
//Here is an instantiation using a static contructor (factory)
SomeObject obj2 = SomeObject.CreateSomeObject(3);
//Getting field value from static field
// Notice that this references the class name, not an instance
int fooValue1 = SomeObject.Foo;
//Getting property value from instance
// Note that this references an object instance
int fooValue2 = obj2.Foo2;
//Instance method must be called through an object
obj2.AddSomething(4); //if default constructor, would return 8
//Static methods must be called through class name
SomeObject.Add(4); //Returns 7
}
Also, check this post out for a deeper look into static classes.