Tag Archives: Static

When is a static constructor in C# called?

A static constructor is invoked by the first of either of the following conditions:

  • Create an instance of the class.
  • Refer any of the static methods of the class.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

For Generic Class, using different type arguments you’ll get different concrete classes, so Static Constructor will be called multiple time (once for each type)

Leave a comment

Filed under C#

Static Classes

Static classes are loaded automatically by the .NET when the program or namespace containing the class is loaded.
The main features of a static class are:
# They only contain static members.
# They cannot be instantiated.
# They are sealed.
# They cannot contain Instance Constructors

If you include instance member in static class then you will get compile time error.

static class CompanyInfo
{
public static string GetCompanyName() { return “CompanyName”; }
public static string GetCompanyAddress() { return “CompanyAddress”; }
}

Leave a comment

Filed under C#