Wait, doesn't the .NET Framework version dictate the language features you have available? Do you doubt me? Open up Visual Studio 2013 and try adding the following code to some class:
public static string ExampleProperty { get; set; } = "Yowza!";
What do you get? I get:
Invalid token '=' in class, struct, or interface member declaration
![]() |
Syntax error on Property initialization in VS 2013 |
Well, what happens when we decompile the assembly created by VS 2015? We get this:
using System;
using System.Runtime.CompilerServices;
namespace BlogPost
{
internal class Program
{
public static string ExampleProperty
{
get;
set;
}
static Program()
{
Program.ExampleProperty = "Yowza!";
}
public Program()
{
}
private static void Main(string[] args)
{
Console.WriteLine(Program.ExampleProperty);
}
}
}
Look familiar? What would happen if we already had a static Program() method? Let's try and find out!
If we start with this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BlogPost
{
class Program
{
public static string ExampleProperty { get; set; } = "Yowza!";
public static string ExampleVariable;
static Program()
{
ExampleVariable = "wow";
}
static void Main(string[] args)
{
Console.WriteLine(ExampleProperty);
Console.WriteLine(ExampleVariable);
}
}
}
We end up with this when we decompile:
using System;
using System.Runtime.CompilerServices;
namespace BlogPost
{
internal class Program
{
public static string ExampleVariable;
public static string ExampleProperty
{
get;
set;
}
static Program()
{
Program.ExampleProperty = "Yowza!";
Program.ExampleVariable = "wow";
}
public Program()
{
}
private static void Main(string[] args)
{
Console.WriteLine(Program.ExampleProperty);
Console.WriteLine(Program.ExampleVariable);
}
}
}
Interesting isn't it? That is part of why I am looking into the internals of the binary files produced by different frameworks and different compilers. Don't get upset when your Dev Lead or Architect won't let you move to the latest and greatest compiler. There is a reason why standards are important in all things.
Keep having fun, and asking questions!