0

I have following code, which is confusing me a lot. The thing is that when static field abc will be initialized by CLR, it will be kinda instantiation of class ABC which will further instantiate class XYZ. My confusion is that in a multi-threaded environment before the threads do anything with the class Program and it's members, CLR would have already initialized field abc which means any threads trying to run DoSomething() method will have to share abc field (which is also an instance of ABC). Does this mean that class XYZ will only be instantiated once since when CLR initialized abc field just once and it's a shared field.

public class XYZ
{
    public XYZ(string nameOfClass)
    {
        Console.WriteLine("XYZ ctor ran");
    }
}

public class ABC
{
    private XYZ xyz = null;
    public ABC(string nameOfClass)
    {
        xyz = new XYZ(nameOfClass);
    }

    public void DoSomething()
    {
        Console.WriteLine("DoSomething Ran");
    }
}


public class SomeClass
{
   static  ABC abc = new ABC("Program");
   public  void Helper()
    {
        abc.DoSomething();
    }

}

public class Program
{

    static void Main()
    {
        SomeClass sc = new SomeClass();
        SomeClass sc2 = new SomeClass();
        for (int i = 0; i < 20; i++)
        {
            new Thread(sc.Helper).Start();
        }
        sc2.Helper();
    }
}
user2913184
  • 590
  • 10
  • 33

2 Answers2

1

Does this mean that class XYZ will only be instantiated once since when CLR initialized abc field just once and it's a shared field.

Yes. There is only one instance of XYZ since there is only one instance of ABC.

I would note that none of the code that you posted shows any thread-unsafe code, so it's not clear what your concern is. It is possible that you could have multiple threads executing DoSomething at the same time.

D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • @D Stanley you are right this code is thread safe even if multiple threads execute DoSomething() at the same time since DoSomething() doesn't change the object state so there is no problem, but thanks for clearing up my doubt. In fact I'm gonna update my code which will make multi-threaded scenario more apparent. – user2913184 Mar 14 '16 at 23:58
0

The static field is initialized at the first access to the class. See MSDN

A class is not initialized until its class constructor (static constructor in C#, Shared Sub New in Visual Basic) has finished running.To prevent the execution of code on a type that is not initialized, the common language runtime blocks all calls from other threads to static members of the class (Shared members in Visual Basic) until the class constructor has finished running.

Kalten
  • 4,092
  • 23
  • 32
  • Well I'm not sure the statement that you gave regarding a class is not initialized until class' static constructor has finished running is valid or not.Since static fields in C# get initialized even before the class' static constructor runs. Take a look at this link. http://stackoverflow.com/questions/8285168/why-static-fields-initialization-occurs-before-the-static-constructor – user2913184 Mar 14 '16 at 22:58
  • I don't know who voted it up. It's not a satisfactory answer. – user2913184 Mar 14 '16 at 23:00