0 votes
in C Sharp by (13.0k points)
Explain Static constructor in C#?

1 Answer

0 votes
by (23.1k points)

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;

 }

}

Related questions

0 votes
asked Oct 18, 2019 in C Sharp by Robin (13.0k points)
0 votes
asked Oct 18, 2019 in C Sharp by Robin (13.0k points)
0 votes
asked May 6, 2020 in C Sharp by SakshiSharma (30.8k points)
0 votes
+1 vote
asked Apr 9, 2020 in C Sharp by GeorgeBell (4.5k points)
0 votes
asked Feb 18, 2020 in C Sharp by rahuljain1 (6.0k points)
...