This is our lesson no 18 and today I will teach you about the concept of Static members in #C classes. As you learned in the previous lessons, the most common way to interact with a class is to create a class An example is (object) and then work on the object. In most cases, this is the whole thing This class is inclusive, meaning you create several different instances of a class and then use each one They are in one part or one purpose.
Concept of Static Members in #C classes
But there are times when you want to have a class that is second to none. If you build from it, or at least without the need to build an object from it, you can use its members and methods.
For example, you can have a class that has a variable and the value of that variable in all cases regardless of the location its use is the same.
These are called static members, static constant to it The reason is that their value at the application level is always the same
A class can be static or have static members and methods. A static constant class You can not prototype a class. Fixed classes are actually a set of members rather than one Class literally.
You may also create a static-not class and define a constant for its members, a class You can prototype non-static-not and create objects from it, but you can not use static-not members Use it on objects.
The following code is an example of how to define Class Static:
public static class Rectangle
public static int CalculateArea (int width, int height)
return width * height;
As you can see, we used the Static keyword to specify a fixed class and We also used this keyword for the Area Calculator method. If for both class and Method, do not use the Static keyword, the C # compiler will get an error. Because a non-fixed member -not (Static cannot be defined in a Static class.
To use the Calculator method, you need to call it directly to the class as in the following code:
Console.WriteLine (“The area is:” + Rectangle.CalculateArea (5, 4)); We can define other application methods within the Class Rectangle class, but perhaps this question Have you ever wondered why we set the width and height parameters directly to the destination method instead of defining them in class?
Did we pass? Because the above class is defined as a static constant, we can get the values
This is an important point to keep in mind when working with Static functions. Instead of the above code, you can change the class, make it possible to change and complete flexibility to your class give. Like example code.
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 defined the class as static-non. Also a function The Construction constructor is defined as the ratio of the values of the Width and height parameters to the constructed instance of the class Gives. Finally, we have defined the Area put out method to calculate the structure statically. This
A good example is the combination of non-fixed and fixed members in a static-non class.