6

In .Net does static class internally create one object or does it not create any object at all. As per Microsoft docs

As is the case with all class types, the type information for a static class is loaded by the .NET Framework common language runtime (CLR) when the program that references the class is loaded. The program cannot specify exactly when the class is loaded. However, it is guaranteed to be loaded and to have its fields initialized and its static constructor called before the class is referenced for the first time in your program. A static constructor is only called one time, and a static class remains in memory for the lifetime of the application domain in which your program resides.

Can we say an object is created implicitly here.? I'm sure that simply writing static class won't create memory for it until static class or any of its members are referenced some where in code. Correct me if i'am wrong.

10
  • Hello! Does your question answer this?stackoverflow.com/questions/6721832/…
    – user8214728
    Dec 23, 2019 at 10:46
  • 1
    Venkata you meant 'does this answers your question' :)
    – Fabjan
    Dec 23, 2019 at 10:47
  • A static class needs a call to the constructor (new myClass) like all other classes. Just a static class there is only one instance of the class instead of having multiple instances.
    – jdweng
    Dec 23, 2019 at 10:47
  • Why would you think an object is created? I don't see how you could get that from the quote.
    – Sweeper
    Dec 23, 2019 at 10:48
  • 1
    Static object is only created if any of its members are in use, check this code and this
    – Fabjan
    Dec 23, 2019 at 10:52

1 Answer 1

10

If I understood your question correctly, you are interested if the static class object is initialized if you don't call it from anywhere in the code.

So, I just created simple console application with static class, and put some Console.WriteLine commands in the constructor like this:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Hello World!");
    }
}
static class SomeClass
{
    static SomeClass()
    {
        Console.WriteLine(GetId(1));
        Console.WriteLine(GetId(2));
    }
    public static string GetId(int Id) { return Id.ToString(); }
}

I got the following output:

Hello World!

Then I run the program with the access to the static class:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Hello World!");
        Console.WriteLine(SomeClass.GetId(3));
    }
}
static class SomeClass
{
    static SomeClass()
    {
        Console.WriteLine(GetId(1));
        Console.WriteLine(GetId(2));
    }
    public static string GetId(int Id) { return Id.ToString(); }
}

And here, my console output was:

Hello World!
1
2
3

Which means that if you don't call the class inside your program, it is not being initialized and the object is not created accordingly. But if you access the class, the object is created before it's accessed first time in the code, that means that constructor creates it when it's first called, without separate initialization, like: var _someClass = new SomeClass();, it is created before first access, and is created only once during the lifetime of your program, and no matter how much times you call it in your code, after first initialization, the instance lives until your software is running, as no matter how much times or where I would use functions or properties from this SomeClass across the program, I would be reusing the same instance, and if you don't call the class within your code, instance is not created at all, and that's what Microsoft docs refer to I suppose.

4
  • Appreciate your efforts!! However, it is guaranteed to be loaded and to have its fields initialized and its static constructor called before the class is referenced for the first time in your program. -- This statement looks opposite to observed results.
    – Arun Kola
    Dec 27, 2019 at 14:53
  • Actually it does not, as "before the class is referenced for the first time in your program" means that if it is actually referenced, it is also guaranteed to be initialized, before that reference is called from the program. But as I understand, it doesn't say anything about the scenario, where you don't reference this static class in your program at all, where apparently it is not initialized, and that I guess has sense of logic.
    – Irakli
    Dec 27, 2019 at 15:07
  • @ArunKola actually, Irakli's answer remains correct with current versions of .Net. If the static class is not referenced, the code from the static constructor is not called.
    – Dash83
    Mar 22 at 13:16
  • Nice, interesting thorough solution
    – levi
    Apr 20 at 20:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.