What is the use of static class in C#
What is the use of static class in C#, and can we use static constructor in non static class ?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Hemant KumarPosted Jul 27, 2012, 6:09 AM
Yes, it is possible.
But we have to create an object of the class inside the static constructor and then initialize the non static member through the object reference.
Code(example):
class Class2
{
int a;
static Class2()
{
Class2 p = new Class2();
p.a = 45;
System.Console.WriteLine(p.a);
}
static void Main()
{
}
}
VulpesPosted Jul 27, 2012, 5:56 AM
It follows that you don't want the class to be capable of instantiation or to be inherited from and a static class automatically allows neither of these things.
However, you can include a static constructor in a static class, just as you can in a non-static class.