From the course: Advanced C#: Object-Oriented Programming
Unlock the full course today
Join today to access over 24,500 courses taught by industry experts.
Static constructors - C# Tutorial
From the course: Advanced C#: Object-Oriented Programming
Static constructors
- [Instructor] C# supports a type of constructor called a static constructor and they apply to both regular and static classes. Static constructors are typically used to initialize any static data in the class or to perform one-time operations before anything else happens in the class. These are called automatically by the .NET framework. You don't need to invoke these yourself. Let's look at some sample code to see how they work. So here in my StaticConstructor folder, let's open up the Program file and MyClass. So I have a class named MyClass, which has two static members. It's called Counter and LastCaller, a constructor, and there is two methods, MethodA and MethodB. MethodA is an instance method while you can see that MethodB is a static method. When these functions are called, the CallCounter variable will be incremented each time. And the LastCaller string will contain the name of the last method to be called in the…