C# 8 will have a breaking change

in #programming7 years ago

This is not the first time a breaking change has hit the language although the last time few probably noticed.

Last breaking change

The last breaking change in the language was around the semantics of foreach/for loops and related to the scope of the loop variable. So given the following:

foreach (var item in itemList)
{
    // .......
}

Before the change the var item variable was scoped outside the loop and after the change it was inside.

// Before
Item item;
foreach (item in itemList)
{
    // .......
}

// After
foreach (item in itemList)
{
    Item item;
    // .......
}

So real subtle stuff that only effected code that made use of lambda and closures.

New breaking change

The new planned breaking change coming has more seismic implications on the languages, how you interact with reference types. In C# 8 all reference types will not be nullable by default, you need to qualify them with ? in the same way as nullable value types.

MyClass instance;        // Error not initialised
MyClass instance = null; // Error can't be null
MyClass? instance;       // Nullable so fine

So why do this? Simple really, null has been described as the billion dollar mistake by one of the people involved with the first C#.

If you start to think this change through you can see the huge benefits. No need for many of those BS null checking lines in your code. You can instantly tell if a reference type can be null and hence you might need to deal with it. Things become explicit.

It is still not clear if the nullable references will require ".Value" semantics to de-reference them but I believe this will not be the case.

Old code

This will be handled by compiler switches and pragmas so you can revert back to the old semantics for older code. The non nullable semantics are handled at the compiler level and hence at run time none of these semantics will exist. This allows for the mix between the two. A good move imho.

For me this will be one of the best changes in the language to date, even if it causes some pain initially. Until now people have use hacks like code contracts or validation/asserts at function entry to enfore this sort of checking, both are sort of hidden away and require you do drill into functions to see what sort of contract is in place.

Happy Coding

Woz

Sort:  

Coin Marketplace

STEEM 0.16
TRX 0.13
JST 0.027
BTC 58470.49
ETH 2617.16
USDT 1.00
SBD 2.39