A colleague sent this video to me today containing a presentation of the upcoming C# 8 features.
For you who do not recognize this guy, he’s Microsofts Program Manager for the C# language, and has been so for many years! Not to be mixed up with Mads Kristensen (who wrote this awesome blog engine), also working at Microsoft, both originating from Denmark.
So, let me give you a little summary if you don’t have the time to watch the video, which btw you really should if you’re a .NET developer like me.
Nullable Reference Types
If you have coded C# before, you probably know about the explicit nullable type operator ‘?’.
int? length = null;
This means that that the integer length
now can be set to null, which the primitive type int
never can be. This was introduced way back in 2005 when the .NET Framework 2.0 was released.
So now, the C# team introduces nullable reference types. This means that you can null for example a string. Well, a string can already be null, you might ask yourself, so what’s the big deal? Intension. I bet all programmers have had their share of the infamous NullReferenceException
, am I right? Enter nullable reference types. Want your string to be nullable by intent? Use string?
. This means that the intent of the string variable is that it can be null, and your compiler will notice this. Using a method on the nullable string variable without first checking for null will cause a compiler error. Awesome!
Async Streams
Since streams are naturally a pushing thing, which means stuff is happening at anytime outside the control of the consumer (think a river and a dam). This might build up to congestion where you need some sort of throttling. With async streams you will get this naturally! The consumer now has a saying if it is ready to consume or not. If you have ever used a message queue, this is probably already a natural thing for you. For distributed transport systems like RabbitMQ, this is a natural thing. You spin up more consumers when the queue get’s bigger and bigger and use a thottling mechanism for the consumer to only consume a couple of messages at a time.
Default Interface Implementations
Now this one is interesting. It means you can implement functionality in an Interface definition.
Say what?
You mean like an abstract class? Well, yes and no. It’s more a usage of the private implicit implementation of an interface. You probably have seen an implicit implementation of an interface before, but let me demonstrate anyway.
public interface IGossip { string TellGossip(); } public class GossipingNeighbor : IGossip { string IGossip.TellGossip() { return "Haha, you won't know about this!"; } } public class NosyNeighbor { private readonly GossipingNeighbor _gossipingNeighbor; public NosyNeighbor(GossipingNeighbor gossipingNeighbor) { _gossipingNeighbor = gossipingNeighbor; } public void PleaseTellMe() { // Compiler error var theMotherLoadOfSecrets = _gossipingNeighbor.TellGossip(); } }
So extending interface definitions in C# 8, you can actually use this directly in the interface definition! If you add a method to the above interface and provide a default implementation, the implementations of that interface does not break, which means they do not need to explicit implement this new method. This does not even affect the implementation until it is casted to the interface.
public void PleaseTellMe() { // Compiler error var theMotherLoadOfSecrets = _gossipingNeighbor.TellGossip(); // This works! var iDidntKnowThat = ((IGossip) _gossipingNeighbor).TellGossip(); }
Default interface implementations was introdued in Java 8 in 2015, so here C# is actually behind Java!
Extend Everything!
Well, maybe not. Since 2007 (.NET Framework 3.5, C# version 3.0) you have had the extension methods. You can add methods to an exiting class definition without touching the class. Formerly this only included methods, now you can use properties, operators and maybe constructors aswell! However, there is limitations. You can not hold instance states, but you can hold definition states, i.e. static states. Maybe not revolutionizing, you can already do a great amount of stuff with extention methods, but still, there will be times where this might be useful.
Mads also talks about extending extionsions with interfaces. He does not go into details what that means, and also states that this probably is way into the future of the evolvement of C#.
Conclusion
No generic attributes?
Still, lot’s of new goodies that might be included in C# 8. However, bear in mind that many of these ideas might not turn out like this when C# 8 is actually released. If you watch the video you’ll hear Mads state the uncertainties of what actually will be shipped, but I think it corresponds quite much to the C# 8 Milestone.
Leave a Comment