The following code throws NullReferenceException on .NET Core (doesn't throw on .NET Framework).
It this a bug?
usingSystem;structS{}classA{staticTimeSpanx=default;// throws NRE// FYI, generated il is: ldsflda, initobj}classB{staticSx=default;// throws NRE}classC{staticSx;staticC()=>x=default;// throws NRE}classD{// no exception if static fiealds are primitives or reference types// generated il is: ldnull/ldc.i4, stsfldstaticstringx=null;staticinty=0;}staticclassProgram{staticvoidMain(){Console.WriteLine("----");try{typeof(A).TypeInitializer.Invoke(null,null);}catch{Console.WriteLine("A");}// Atry{typeof(B).TypeInitializer.Invoke(null,null);}catch{Console.WriteLine("B");}// Btry{typeof(C).TypeInitializer.Invoke(null,null);}catch{Console.WriteLine("C");}// Ctry{typeof(D).TypeInitializer.Invoke(null,null);}catch{Console.WriteLine("D");}// (no excpetion)Console.WriteLine("----");Aa=newA();Bb=newB();Cc=newC();try{typeof(A).TypeInitializer.Invoke(null,null);}catch{Console.WriteLine("A");}// Atry{typeof(B).TypeInitializer.Invoke(null,null);}catch{Console.WriteLine("B");}// Btry{typeof(C).TypeInitializer.Invoke(null,null);}catch{Console.WriteLine("C");}// (no excpetion)try{typeof(D).TypeInitializer.Invoke(null,null);}catch{Console.WriteLine("D");}// (no excpetion)}}
The following code throws NullReferenceException on .NET Core (doesn't throw on .NET Framework).
It this a bug?