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.