Tutorial by Examples

When a type is defined without a constructor: public class Animal { } then the compiler generates a default constructor equivalent to the following: public class Animal { public Animal() {} } The definition of any constructor for the type will suppress the default constructor genera...
public class Animal { public string Name { get; set; } public Animal() : this("Dog") { } public Animal(string name) { Name = name; } } var dog = new Animal(); // dog.Name will be set to "Dog" by default. var cat = new Ani...
A static constructor is called the first time any member of a type is initialized, a static class member is called or a static method. The static constructor is thread safe. A static constructor is commonly used to: Initialize static state, that is state which is shared across different instan...
A constructor of a base class is called before a constructor of a derived class is executed. For example, if Mammal extends Animal, then the code contained in the constructor of Animal is called first when creating an instance of a Mammal. If a derived class doesn't explicitly specify which constru...
When an object graph is finalized, the order is the reverse of the construction. E.g. the super-type is finalized before the base-type as the following code demonstrates: class TheBaseClass { ~TheBaseClass() { Console.WriteLine("Base class finalized!"); } } ...
public class SingletonClass { public static SingletonClass Instance { get; } = new SingletonClass(); private SingletonClass() { // Put custom constructor code here } } Because the constructor is private, no new instances of SingletonClass can be made by consum...
While static constructors are always called before the first usage of a type it's sometimes useful to be able to force them to be called and the RuntimeHelpers class provide an helper for it: using System.Runtime.CompilerServices; // ... RuntimeHelpers.RunClassConstructor(typeof(Foo).TypeHand...
Unlike C++ in C# you can call a virtual method from class constructor (OK, you can also in C++ but behavior at first is surprising). For example: abstract class Base { protected Base() { _obj = CreateAnother(); } protected virtual AnotherBase CreateAnother() { ...
If the type on which the static constructor is declared is generic, the static constructor will be called once for each unique combination of generic arguments. class Animal<T> { static Animal() { Console.WriteLine(typeof(T).FullName); } public static void Yawn...
If a static constructor throws an exception, it is never retried. The type is unusable for the lifetime of the AppDomain. Any further usages of the type will raise a TypeInitializationException wrapped around the original exception. public class Animal { static Animal() { Consol...
Shall the property value's assignment be executed before or after the class' constructor? public class TestClass { public int TestProperty { get; set; } = 2; public TestClass() { if (TestProperty == 1) { Console.WriteLine("Shall this be e...

Page 1 of 1