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.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
Arun Kola
  • 69
  • 7
  • Hello! Does your question answer this?https://stackoverflow.com/questions/6721832/c-sharp-static-class-constructor –  Dec 23 '19 at 10:46
  • 1
    Venkata you meant 'does this answers your question' :) – Fabjan Dec 23 '19 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 '19 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 '19 at 10:48
  • "its static constructor called before the class is referenced for the first time in your program." CLR create one and only one instance of a static class. – Krivitskiy Grigoriy Dec 23 '19 at 10:51
  • 1
    Static object is only created if any of its members are in use, check this [code](https://dotnetfiddle.net/A2SILm) and [this](https://www.codeproject.com/Questions/1206603/When-exactly-does-memory-allocation-occur-for-stat) – Fabjan Dec 23 '19 at 10:52
  • Static types are never *created*, but they *are* **initialized**, as the documentation quoted states. This initialization happens before the members of the type is first used. – Lasse V. Karlsen Dec 23 '19 at 11:39
  • The memory necessary to hold the variables of the static type is set aside when the program starts, regardless of when or if the members are actually used, but the actual initialization is postponed until "right before its needed". – Lasse V. Karlsen Dec 23 '19 at 11:41
  • @LasseV.Karlsen: Thanks for info, i got your statement about if Static type is initialized or created. But still confused about when it is initialized. Your comments are conflicting. – Arun Kola Dec 27 '19 at 14:50
  • Was my answer satisfactory for your question? – Irakli Aug 19 '21 at 09:12

1 Answers1

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.

Irakli
  • 562
  • 4
  • 15
  • 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 '19 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 '19 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 '23 at 13:16
  • Nice, interesting thorough solution – levi Apr 20 '23 at 20:13