Static Members
The static keyword is used on members and properties that want to be shared by all instance of the class. When a method or property is defined as static, you can call them without creating an instance of that object. Let’s define some static methods and properties.
using System;
namespace StaticMembersDemo
{
class SampleClass
{
public static string StaticMessage { get; set; }
public string NormalMessage { get; set; }
public static void ShowStaticMessage()
{
Console.WriteLine(StaticMessage);
}
public void ShowNormalMessage()
{
Console.WriteLine(NormalMessage);
}
public void ShowStaticFromInstance()
{
Console.WriteLine(StaticMessage);
}
}
class Program
{
public static void Main()
{
SampleClass sample1 = new SampleClass();
SampleClass sample2 = new SampleClass();
SampleClass.StaticMessage = "This is the static message!";
SampleClass.ShowStaticMessage();
sample1.NormalMessage = "
Message from sample1!";
sample1.ShowNormalMessage();
sample1.ShowStaticFromInstance();
sample2.NormalMessage = "
Message from sample2!";
sample2.ShowNormalMessage();
sample2.ShowStaticFromInstance();
}
}
}
Example 1 – Demonstrating Static Members
This is the static message! Message from sample1! This is the static message! Message from sample2! This is the static message!
We declared a static property named StaticMessage(line 7) and a static method named ShowStaticMessage(lines 10-13). The value of the StaticMessage property will be shared by all instance of the SampleClass. The static method cannot be called by instances of the SampleClass. To call the static method or property, simply write the name of the class and the property or method name separated by a dot. You can see this in lines 33-34. No instance creation is needed.
We assigned a message for the static message and call the static method to display its value. Next we call the NormalMessageproperty from the two instances and call the method that shows its value. We see that both of the two instances have their own NormalMessage value since that property is not static. We then call the ShowStaticFromInstance which is a method that will show the value of the StaticMessage to show that it still has the value assigned when we called it directly through the name of the class. Non-static methods can use static fields and properties. But the reverse is not allowed. For example, you have a static method, you cannot use any properties, fields or methods that are not labeled as static.
private int number = 10;
public static void ShowNumber()
{
Console.WriteLine(number); //Error: cannot use non-static member
}
Insisting on doing this will result in an error. An example of a static method is the WriteLine method of the Console class. If that particular method is not static, then we have to do the following:
Console display = new Console();
display.WriteLine("My message!");
We will need to create an instance of the Console class first before we can display messages. Another thing to note is the Main()method declared as static. The compiler requires it to be static so it can be executed in a program without creating an instance of it.