I'm using VisualStudio 2017 on a project that targets .NET Framework 4.6.1.
Playing with Task, CancellationToken and a local method, I came to this code:
class Program
{
    static void Main(string[] args)
    {
        CancellationToken o;
        Console.WriteLine(o);
    }
}
Which compiles. Now if you change the type of o to an int or a string it won't compile, giving the error:
Local variable 'o' might not be initialized before accessing.
I tried to decompile CancellationToken and copied the code in a struct named MyCancellationToken. That won't compile either.
The only case I managed to make compile is an empty struct or a struct containing a CancellationToken.
struct EmptyStruct
{
}
struct MagicStruct
{
    public CancellationToken a;
}
class Program
{
    static void Main(string[] args)
    {
        EmptyStruct a;
        MagicStruct b;
        Console.WriteLine(a);
        Console.WriteLine(b);
    }
}
- Why doesn't CancellationTokenneed initialisation?
- What are the other types that don't need initialisation?
Fun fact: if you mix this with a local function you can write this:
class Program
{
    static void Main(string[] args)
    {
        Work();
        CancellationToken o;
        void Work() => Console.WriteLine(o);
    }
}
Which compiles.
 
    