Exactly because that's the whole point of static methods.
Instance methods require to know which instance of the class you call the method on.
instance.Method();
and they can then reference instance variables in the class.
Static methods, on the other hand, don't require an instance, but can't access instance variables.
class.StaticMethod();
An example of this would be:
public class ExampleClass
{
public int InstanceNumber { get; set; }
public static void HelpMe()
{
Console.WriteLine("I'm helping you.");
// can't access InstanceNumber, which is an instance member, from a static method.
}
public int Work()
{
return InstanceNumber * 10;
}
}
You could create an instance of this class to call the Work()
method on that particular instance
var example = new ExampleClass();
example.InstanceNumber = 100;
example.Work();
The static
keyword though, means that you don't need an instance reference to call the HelpMe()
method, since it's bound to the class, and not to a particular instance of the class
ExampleClass.HelpMe();