| Start |
|---|
| Static constructor 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:
Example: Output: Animal initialized If the first call is to a static method, the static constructor is invoked without the instance constructor. This is OK, because the static method can't access instance state anyways. This will output: Animal initialized See also Exceptions in static constructors (opens new window) and Generic Static Constructors (opens new window) . Singleton example: |
Singleton constructor pattern Because the constructor is private, no new instances of The Note, that if the static constructor fails the Also, the static constructor is not guaranteed to run at the time of the first access of See the Singleton Implementations (opens new window) page for other ways to implement the singleton pattern. |
| Default Constructor When a type is defined without a constructor: then the compiler generates a default constructor equivalent to the following: The definition of any constructor for the type will suppress the default constructor generation. If the type were defined as follows: then an For the second example, the compiler will display an error message: 'Animal' does not contain a constructor that takes 0 arguments If you want a class to have both a parameterless constructor and a constructor that takes a parameter, you can do it by explicitly implementing both constructors. The compiler will not be able to generate a default constructor if the class extends another class which doesn't have a parameterless constructor. For example, if we had a class then |
| Forcing a static constructor to be called 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 Remark: All static initialization (fields initializers for example) will run, not only the constructor itself. Potential usages: Forcing initialization during the splash screen in an UI application or ensuring that a static constructor doesn't fail in an unit test. |
Calling a constructor from another constructor |
| Calling the base class constructor A constructor of a base class is called before a constructor of a derived class is executed. For example, if If a derived class doesn't explicitly specify which constructor of the base class should be called, the compiler assumes the parameterless constructor. In this case, instantiating a An unknown animal gets born. Calling a different constructor of the base class is done by placing Calling George the Cat gets born. |
| Finalizers on derived classes 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: |
| Exceptions in static constructors 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 This will output: Static ctor System.TypeInitializationException: The type initializer for 'Animal' threw an exception. ---> System.Exception: Exception of type 'System.Exception' was thrown. [...] System.TypeInitializationException: The type initializer for 'Animal' threw an exception. ---> System.Exception: Exception of type 'System.Exception' was thrown. where you can see that the actual constructor is only executed once, and the exception is re-used. |
| Calling virtual methods in constructor 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: If you come from a C++ background this is surprising, base class constructor already sees derived class virtual method table! Be careful: derived class may not been fully initialized yet (its constructor will be executed after base class constructor) and this technique is dangerous (there is also a StyleCop warning for this). Usually this is regarded as bad practice. |
| Generic Static Constructors 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. This will output: System.Object See also How do static constructors for generic types work ? (opens new window) |
| Constructor and Property Initialization Shall the property value's assignment be executed before or after the class' constructor? In the example above, shall the Assigning property values in the instance creation like this: Will be executed after the constructor is run. However, initializing the property value in the class' property in C# 6.0 like this: will be done before the constructor is run. Combining the two concepts above in a single example: Final result: Explanation: The And then the RemarksC# does not actually have destructors, but rather Finalizers which use C++ style destructor syntax. Specifying a destructor overrides the Unlike other languages with similar syntax, these methods are not called when objects go out of scope, but are called when the Garbage Collector runs, which occurs under certain conditions (opens new window). As such, they are not guaranteed to run in any particular order. Finalizers should be responsible for cleaning up unmanaged resources only (pointers acquired via the Marshal class, received through p/Invoke (system calls) or raw pointers used within unsafe blocks). To clean up managed resources, please review IDisposable, the Dispose pattern and the (Further reading: When should I create a destructor? (opens new window)) |
Read Article
https://develop.horje.com/learn/1434/reference