Categories

Oct 18, 2019 in C Sharp (C#)

Q: Explain Static constructor in C#?

1 Answer

Oct 18, 2019

If the constructor is declared as static then it will be invoked only once for all number of instances of a class. Static constructor will initialize the static fields of a class.

class MyClass

{

 public string prop1, prop2;

 public MyClass(string a, string b)

 {

 prop1 = a;

 prop2 = b;

 }

Static MyClass()

 {

 Console.WriteLine(“Static Constr Test”);

 }

 public MyClass(MyClass myobj) // Copy Constructor

 {

 prop1 = myobj.prop1;

 prop2 = myobj.prop2;

 }

}

Click here to read more about C Sharp
Click here to read more about Insurance

Related questions

Madanswer
Oct 18, 2019 in C Sharp (C#)
Oct 18, 2019 in C Sharp (C#)
Oct 18, 2019 in C Sharp (C#)
...