By the will of fate in recent years, I have got another very exciting hobby - teaching. I train people who want to become C # programmers. People come different: techies, humanists, someone of their own free will, someone sent from organizations. Despite the different levels, I need to train them. Therefore, I try to constantly update and improve my teaching materials. In this connection, he came to the conclusion: “But it would not be bad to arrange the materials in text form, so that it would be convenient to use them”. Under the cut, I laid out one of the recently issued lectures as an example.
General concept
First of all, you should understand what is static. C # has a static keyword that can be applied to:
- fields
- properties
- methods
- to operators
- events
- to the designer
- classes
static class OneHuman { public static string Name { get; set; } public static int Age { get; set; } public static string GetInformation() { return string.Format("{0} is {1} years old", Name, Age); } }
')
Static implies that you do not need to create an instance of a class. All the above class components are available by specifying its name.
class Program { static void Main(string[] args) { OneHuman.Name = "Onizuka"; OneHuman.Age = 22; Console.WriteLine(OneHuman.GetInformation()); } }
It should be noted that it is not necessary to make the whole class static. Sometimes it is enough to apply statics for its individual members.
class Box { public static string DefaultContext { get; set; } public string Context { get; set; } }
If the entire class is static:
- You cannot create an instance of a class using the new keyword.
- It is not allowed to use non-static members of the same class.
- It does not support inheritance.
- Unable to overload methods.
If the class is not static, but contains static methods, then the following restrictions apply to these methods:
- It is not allowed to use non-static members of the same class from static ones. Of course, no one bothers you to create an instance of a class in a static method.
- Inheritance and polymorphism for static members are not supported.
More details
Above, we did not consider such a construction as a static constructor. One of the rather interesting questions, in my opinion, when does the static constructor call the classes?
class Box { static Box() { Console.WriteLine("static ctor was called"); } public Box() { Console.WriteLine("default ctor was calleds"); } public static string Data { get { return "You are trying to get data"; } } public const int Foo = 7; }
I think you have already noticed that the access specifier is not used for the static constructor. Everything is very obvious, you do not control the creation of statics. If you try to execute the code below, you can make sure that the following statement is true: the static constructor is called before accessing any member of the class.
class Program { static void Main(string[] args) { var data = Box.Data; var box = new Box(); } }
You can play by commenting out any of the lines in which the Box class is accessed. Now let's change the code a bit, and correct our statement.
class Program { static void Main(string[] args) { var data = Box.Foo; } }
In this case, the static constructor call does not occur. So: the
static constructor is called before accessing any member of the class, with the exception of constants . I knowingly used the word class in this definition. With the structures a lot of "jokes". You should be aware that in C # you cannot override the default constructor, but you can define a static constructor without parameters. However, it will not always be called, for example, it will not be called if, for example, you try to create an array of structures.
General reasoning on the use of statics
There are relatively many opinions about when to use static classes and when you shouldn't do that. Based on my experience, I will note that static classes are the favorite weapon of novice developers. I used and forgot - a good concept.

To understand the intricacies of the applicability of statics, one should return to the concepts of OOP. Imagine that you have a bike, but your neighbor has a bike, a neighbor, and so on. In this case, the static is unacceptable. Since Bicycles can be of different colors, weights, have a different number of wheels. That is, different instances of the same species. Statics is applicable for some global objects \ actions, when the creation of class instances is not implied (often for some service methods: console output - Console.WriteLine (), Array.Sort array sorting). Often classes can provide both static and non-static functionality. When you have doubts, stop and think about whether you need a copy of “this”. If you so want to control the creation of instances of a class, or even have only one, then the
Singleton pattern is remarkably useful for these purposes. As part of the PLO, statics has a number of drawbacks. Why is she so bad?
Polymorphism
As mentioned above, static classes do not support inheritance, i.e. you cannot inherit from an interface or another class and thus extend the functionality.
Testing
When using statics testing is quite difficult. It is impossible to promptly replace code based on interfaces. If you need to change, then seriously, rewriting significant pieces of code.
Sole responsibility
Static methods are often used for official purposes, whether it be output to the error log or sorting an array. Therefore, sometimes there is a desire to cram all static functionality into one class, calling it MegaUtils. This is not worth doing, it is better to create a whole bunch of small classes, each responsible for their field of activity.