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 ?
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
VulpesRank #55 · 29.2K reputationPosted Jul 27, 2012, 1:56 AM EDT
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.
Hemant KumarRank #1,202 · 1.5K reputationPosted Jul 27, 2012, 2:09 AM EDT
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()
{
}
}