The article is about the static keyword in c#
A static method or field is one that belongs to the class and no instance can have access to the member and methods.
Let’s create an example
public class Account {
private static int minAge = 18;
private static int minIncome = 10000;
public static bool AccountAllowed (decimal income, int age)
{
if (income > minIncome && age >= minAge)
{
return true;
}else
{
return false;
}
}
}
Here we have a static method AccountAllowed. In the code above you can see that we have also made the members minAge and minIncome static aswell otherwise we would not be able to have access to the minAge and minIncome method in the AccountAllowed method.
You can see that the IDE is not showing the AccountAllowed method in the available methods on the instance.
If we didn’t make the dependant members static we would recieve an error CS0120 which is just saying “a static method is using a member of the class which is not static”.
You can also have static classes. A static class cannot be instantiated and all it’s members must also be static. You might be wondering why would we use static classes at this point. They are useful for creating utility functions. Let me give you an example from the C# language itself.
static void Main()
{
Math.min(10,4) // would give 4 as the return value
Math.max(10,4) // would give 10 as the return value
}
As you can see from this example the all the methods are directly called on the Math class itself it doesn’t need to create an instance to use the methods. This shows us when and where a static class can be useful.
Why can’t static members have access to Object properties ?
The simple answer is that .NET compiler does not gives access to the this keyword. The this keyword allows you to access properties of the current instance of the class. When you declare a member with static keyword the .NET compiler doesn’t let you have access to the this reference.