Out of curiosity:
This code is valid and executes:
public class Program
{
private static DateTime date;
public static void Main()
{
Console.WriteLine(date.ToString("o"));
}
}
See it working on .NET Fiddle
But this doesn´t even compile (unassigned local variable):
public class Program
{
public static void Main()
{
DateTime date;
Console.WriteLine(date.ToString("o"));
}
}
See it (not) working on .NET Fiddle
DateTime is a non nullable value type, so it doesn´t need to be assigned and initialized to have a value, it has a default one.
So why the compiler allows the DateTime field version compile and doesn´t let the local variable version compile? When the code is compiled to IL, what prevents an value type local variable from being used?