Wednesday, 7 December 2016

19 C# Static members

Static members

As we saw in a previous chapter, the usual way to communicate with a class, is to create a new instance of the class, and then work on the resulting object. In most cases, this is what classes are all about - the ability to instantiate multiple copies of the same class and then use them differently in some way. However, in some cases, you might like to have a class which you may use without instantiating it, or at least a class where you can use members of it without creating an object for it. For instance, you may have a class with a variable that always remains the same, no matter where and how it's used. This is called a static member, static because it remains the same. 

A class can be static, and it can have static members, both functions and fields. A static class can't be instantiated, so in other words, it will work more as a grouping of related members than an actual class. You may choose to create a non-static class instead, but let it have certain static members. A non-static class can still be instantiated and used like a regular class, but you can't use a static member on an object of the class. A static class may only contain static members. 

First, here is an example of a static class:
public static class Rectangle
{
    public static int CalculateArea(int width, int height)
    {
        return width * height;
    }
}
As you can see, we use the static keyword to mark the class as static, and then we use it again to mark the method, CalculateArea, as static as well. If we didn't do that, the compiler would complain, since we can't have a non-static member of a static class. 

To use this method, we call it directly on the class, like this:
Console.WriteLine("The area is: " + Rectangle.CalculateArea(5, 4));
We could add other helpful methods to the Rectangle class, but perhaps you are wondering why we are passing on width and height to the actual method, instead of storing it inside the class and then pulling them from there when needed? Because it's static! We could store them, but only one set of dimensions, because there is only one version of a static class. This is very important to understand. 

Instead, we can make the class non-static, and then have the CalculateArea as a utility function on this class:
public class Rectangle
{
    private int width, height;

    public Rectangle(int width, int height)
    {
        this.width = width;
        this.height = height;
    }

    public void OutputArea()
    {
        Console.WriteLine("Area output: " + Rectangle.CalculateArea(this.width, this.height));
    }

    public static int CalculateArea(int width, int height)
    {
        return width * height;
    }
}
As you can see, we have made the class non-static. We have also added a constructor, which takes a width and a height and assigns it to the instance. Then we have added an OutputArea method, which uses the static method to calculate the area. This is a fine example of mixing static members with non-static members, in a non-static class. 

A common usage of static classes, although frowned upon by some people, are utility/helper classes, where you collect a bunch of useful methods, which might not belong together, but doesn't really seem to fit elsewhere either.

No comments:

Post a Comment