C#, Tech

C# 8 features – part 1.

If you were online at least once in the previews week you probably noticed the biggest announcement from Microsoft! Yeah, everybody is talking about C# 8 planned features. And because the news is literally everywhere, I gave up and decided not to repeat what everybody already read. But that decision is no longer valid because back THEN (which means – yesterday or so) my pet project wasn’t so nasty and I wasn’t so stuck in the middle of some stupid, trivial task that seems to be unable to accomplish… So yeah, I changed my mind, I will wrote about C# 8, just to avoid touching (for at least a day 😉 ), the disaster I programmed :D. No, it’s not procrastination, not at all! 😉

First things first!

It is said that C# 8 will appear as part of Visual Studio 2019 previews, probably not all the features at the same time. What’s interesting, .Net Core 3.0 will ship in the same manner, so we will receive a lot of toys to play and that literally makes me smile 😊! What sounds even better I remember (but don’t trust my memory too much) reading somewhere that VS 2019 preview will be available before the end of this year, so you know, ‘the Santa is coming to town’ :D.

But let’s get to some ‘meat’… Nullable reference types

The biggest ‘wow’ of course is nullable reference types and I already wrote about it here. What surprised me, is the fact that assigning a ‘null’ to non-nullable type will not cause a compile error. So, when you open an old project and upgrade it to C# 8, you won’t end up with thousands of errors. Only thousands of warnings :D. But hey, at least it compiles, right? But to be honest, I am grateful Microsoft chose warnings above errors. Not being able to upgrade C# language in a huge legacy project sounded badly.

Another interesting announcement suggests that compiler will analyze the code flow and thanks to that the warnings will appear only in places where null occurrence will be really possible. Take a look at the code below.

Avoiding type name when creating an instance

Well, it sucks when we have to write all this:

And it just gets only more irritating when we have to multiple the above line:

Because Microsoft is nice and loves C# programmers, they promised us a solution for the above problem – with C# 8 you don’t have to write type name, where it can be deducted:

Nice, isn’t it?

Interfaces with default implementation

Yeah, interface will be able to have a method’s body that can be overridden (or not) in the implementing classes (the decission is up to a class’ programmer).

Microsoft states that this will allow us to add some extra methods to interfaces that were already implemented in many places and we will not have to implement that method in all these places. Makes sense but… I have a mixed feeling here. Interface was so clean and nice thing for so long and all this messy stuff was usually placed in an abstract class… And now this golden rule will be broken! Can you imagine? Not to mention, all these StackOverFlow questions ‘what’s the difference between interface and abstract class’… Who will rewrite the answers, who?!😉

That’s all for today. In the next post I hope to write about the rest of C# 8 features, so stay tunned!

 

Featured photo by Jordan Rowland on Unsplash

Share this: