Static members in C#

As we saw in the previous chapter, the common method of communicating with one class is to create a new example of the class, and then to work on the resulting object. In most cases, this is the same class which is all about - The ability to change many copies of the same class instantly and then somehow use them differently, however, in some cases, you want a square, which you use no-tattoos C, or at least can make a square, where you can do it without using its members maintain an object for it. For example, you might have a class with a variable that is always the same, even if it's used and how. It is called stable member, stable because it remains the same

A class can be stable, and can have stable members in it, both tasks and areas. A steady class can not be done immediately, so in other words, it will work more as a group of related members compared to the actual class. You can choose to create a non-static class instead, but there are some stable members in it. A non-static class can still be used like an immediate and regular class, but you can not use a stable member on an object in the class. Only a stable member can be a stable 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 still use static keywords to mark the enduring keywords, and then we re-use it to stabilize the method, calculatoria if we do not do so, compiler complains Because we can not be non-stable members of a stable class.

To use this method, we call it straight in class, such as:


Console.WriteLine("The area is: " + Rectangle.CalculateArea(5, 4));

We can add other useful ways in the rectangle, but perhaps you are thinking that instead of depositing it in the classroom, why are passing the width and height for the actual method, and then drag them when needed? Because it's stable! We can store them, but only a single set of dimensions, because there is only one version of a stable class, it is very important to understand.

Instead, we can make the class non-static, and then calculate this class as a utility function:


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 the width and height and specifies it for example. Then we've added the output field method, which uses the static method to calculate the area. This is a great example of mixing stable members with stable members in non-stable classes.

However, a common use of stable classes is harassed by some people, utility / supportive classes, where you collect a set of useful methods that can not be together, but are not really fit anywhere