What's New in C# 8.0

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

My jaw is still on the ground. So many good features in there.

πŸ‘οΈŽ︎ 28 πŸ‘€οΈŽ︎ u/MaximRouiller πŸ“…οΈŽ︎ Dec 06 2018 πŸ—«︎ replies

I could have used case expressions countless times over the last 22 years

πŸ‘οΈŽ︎ 20 πŸ‘€οΈŽ︎ u/ziplock9000 πŸ“…οΈŽ︎ Dec 06 2018 πŸ—«︎ replies

So excited for these features! c# has come a long way!

πŸ‘οΈŽ︎ 3 πŸ‘€οΈŽ︎ u/jpgrassi πŸ“…οΈŽ︎ Dec 06 2018 πŸ—«︎ replies

At 8:25, Mads uses range syntax ^1 to indicate "1 from the end". The last time I read about this feature, the indexes were 1 indexed when referring to the end of the array. So, 1..^1 would actually only omit the first element, and not one from each side.

Was this feature changed, or was Mads mistaken?

Edit: After looking into it a bit, it seems this feature was changed. At BUILD this year, they used ^1 to refer to the last element, but in this MSDN doc (published yesterday), they use ^0.

πŸ‘οΈŽ︎ 3 πŸ‘€οΈŽ︎ u/MaxxDelusional πŸ“…οΈŽ︎ Dec 06 2018 πŸ—«︎ replies

Im quite new to C# and these concepts. Can anyone offer some different practical uses for this new syntax? Edit: a word

πŸ‘οΈŽ︎ 6 πŸ‘€οΈŽ︎ u/watmmwatdd πŸ“…οΈŽ︎ Dec 06 2018 πŸ—«︎ replies

I'm glad C# is improving in big ways, and relatively quickly compared to other popular languages, especially Java, but I can't help but take issue with some of these features. Get your downvotes ready, lads.

Non-nullable references seem half-assed to the point that they're barely better than using ReSharper and sprinkling [CanBeNull] and [NotNull] attributes everywhere. I get the reasoning that they don't want to break existing code on an upgrade, but then why is it still a warning even after opting-in to these new semantics? It should be a compiler error without additionally having to tell the compiler to treat that specific warning as an error. It's like they said "fine, you can have your fancy non-nullable references, but you have to opt-in twice to get there".

I also really hate default interface implementations. It's redundant with abstract classes and adds pseudo multiple inheritance (which I also hate) which I have to assume was excluded from C# by deliberate design choice when the language came to be. And the reason given about forward compatibility when an interface changes all existing implementations don't necessarily have to change, combined with the double opt-in of non-nullable references makes it sound like Microsoft is going the C++ route of "never ever ever ever break compatibility ever, even when it makes sense for the long-term", and I don't think I need to explain why that's not a good thing.

Also records weren't mentioned in the video, have they been pushed back again, or is this just an incomplete list?

πŸ‘οΈŽ︎ 3 πŸ‘€οΈŽ︎ u/[deleted] πŸ“…οΈŽ︎ Dec 06 2018 πŸ—«︎ replies

I find default interface members to be highly questionable.

πŸ‘οΈŽ︎ 1 πŸ‘€οΈŽ︎ u/evil_boob πŸ“…οΈŽ︎ Dec 07 2018 πŸ—«︎ replies
Captions
hi I'm master Gerson and I'm the lead designer for c-sharp I want to welcome you to Microsoft Connect and share with you some of the major features that are coming into C sharp 8 the first of which you can actually try out in their preview that's come out at the time of this event so I'm going to show as many hours in the preview I'm going to show you in Visual Studio kind of head over there right now and for the remainder we'll take a quick tour in slides and they will show up in later previews and of course in the final product so the first thing I want to talk about is null which has you know cost many an exception and I'll reference exception in production code sometimes and which we really want to do a better job in c-sharp helping you avoid so if you look at the code here just a quick run-through of the flow I'm getting some subscribers I'm getting the names of those subscribers through a helper method which in turn is getting the name of each subscriber which in turn is constructing a string based on the data the strings that are inside of the of the person class that that is in the subscribers collection so let's just go ahead and run this code and have some fun seeing all these nice names being printed out and whoops to came out but now of course we have a null reference exception the null reference exception if we can scroll a little here turns out that it is in that code producing the string and of course it's the middle name that turns out to be null in the the third person I was trying to render here and therefore indexing into it leads to no reference exception well wouldn't it have been nice if I had known ahead of time not when my customer sent in this bug report so that's exactly what novel reference types are about as a feature essentially novel reference types are about solving the problem of finding out where null should be and where they shouldn't be and tracking where they will be and won't be so to make that that concrete let's start out by actually enabling the nullable reference types feature and i need to enable it i can enable it for the whole project here i'm just going to do it in source code so you can turn it on and off and the reason why it needs to be enabled unlike all other c share language features is that it will give new warnings and existing code and we don't want to do that to you when you upgrade except when you opt into that so you can see that when I type nullable enable here I got a little green smudge here that's a warning that showed up in my code because I Nabal Danu feature so let's go and see what that warning is all about it says that this constructor does not initialize the none nullable property middle name well none nullable what does that mean well it means that when you turn this feature on forevermore in c-sharp we consider your your reference types your ordinary reference types to be none nullable we will stop letting you put an all into them so that seems a little harsh maybe but it doesn't sure that you won't get that no later so it might not be such a bad choice so I'm not initializing it maybe I could initialize it with null instead and make that one and go away and indeed this warning goes away I'm now initializing it but I get another warning saying you can't put an all into this it's an unknowable reference type well what if I want my nulls there there they in the language for a reason you can't just ban them and sure enough you can't have nulls but now you have to own up to it you have to express we're expressing here in a public API yes we will have nulls beware here might be nulls okay so we've taken responsibility and told the rest of the world that nulls can be here and now on the receiving end the line of code up here that is trying to index into the middle name now we get a warning here saying oh there's possible dereference of null can you do about that well you can do what you always did you can check for null before you dereference so I can say if P dot middle name it's not equal to no before I return that and you see that the warning goes away so essentially the compile is tracking where it's tracking peated middle name here and anything else inaudible and tracking where might it actually be null and you just checked and so they wasn't no so on this branch here can't be null and that goes regardless of how you check for null today compile is doing its best to track that and remove those warnings when it sees that you have actually checked properly and ensured there's no null in there so another way that we could be doing this is that we could actually we could do it as a conditional ternary expression we could return that question mark this copy and paste put a colon so bear with me as I reformat my code but you can see now on the two branches of the conditional one of them gets the warning and one doesn't depending on whether the compiler sees that it might be no Lynott and of course and the one where it might be no we'll just remove it from the result and return a string that doesn't make use of of the middle name that isn't there okay so that's Noble reference types the next thing I want to share is async streams so in reality here so I'm I'm iterating over the subscribers I'm kind of imagining this is sort of a live thing like whenever somebody subscribes I get the name as part of a stream and then I get them as part of a stream then I get the name of that and I print that out so this happens over time asynchronously we we're lacking a notion of a syncs streams in c-sharp today and and we're doing something about that and integrating that into the language so let's say that I get an async version of subscribers and instead of being an ienumerable now then I async enumerable that's like an enumerable it can be reached ish but in its it's one that's a sink so you sometimes have to wait the next element and of course now I need to change get names to take an is sink in numerable and now for each won't work because you can't for each over a sink Norma Bowles but you can't wait for each over them so that's a little bit of new syntax that lets you await let you for each over asynchronous streams of course I can only await if I'm inside of an async method but an async method can't return in I know mobile it has to return something async but that async thing can be another a singer' mobile so now you're getting an asynchronous stream of the results here and if you look up here now I have to also await for each the result of this call and I can only await if I'm in an async method but I can make main methods async now as of c-sharp seven two and so now my code is back to working again and if we try running it you can see that as these names trickle in over time the you know the results are produced and print it out and that for each loop is just sort of a waiting for every round with some delay and there we go we've iterated the list that's produced and the program terminates so that's async streams the last feature I want to show you directly in code it's a tiny little one but we think it's going to be very useful inside of my fake service here that I've been using all along I'm actually creating an array of people and then give yielding that bag out now wouldn't it be nice if I could easily specify a sub range as slice if you will of that array and you can do that now with with the range syntax so I can say I only want to get from one to three out of this array and actually the syntax were saying well I want one from the beginning and one from the end and then I'm sort of slicing off the the outermost elements so the hat there means from end and that's the that's the async or sorry that's the ranges feature in c-sharp and that's pretty much it for the preview so let's go back to the slides and see some of the features that will show up in subsequent releases so we saw this code here in the in the in the example before where I'm choosing between whether the middle name is null or not but actually the first name or the last name should probably also be able to be null it's not always that people have first names or last names or maybe they have the names but they weren't recorded here or they didn't want to give them or whatever so we should expand this logic to more like a switch over all the different combinations and you can now do that using what we call recursive patterns so I'm switching over a tuple of the three strings here and then in my cases I can put a pattern that also looks like a tuple we call this a tuple pattern but inside of it it has other patterns which are then used to match against the elements of the tuple so here I'm matching where there are none null strings and here's the case for matching when the middle one is null of course we should also think about when the last name is the one that's null or you know when both a null or actually when the first name is null or you know there's actually eight different cases here and so your switch gets kind of clunky now and we've long filled that switch was a little long in the tooth in terms of being heavy syntactically so we decided now's the time to add switch expressions which is sort of a spiffed up version of switches that are an expression form so here's that same code written with with switch expressions so the switch expression here is everything but the return and the semicolon it has an expression then the switch keyword after that so it composes better with other expressions and then a list of cases that are just a pattern and arrow and an expression for the result and I think it kind of looks nice here that you can do these one-liners you can do this tabular sort of layout and really see you can sort of visually check that we handled all the cases here but beyond that actually because an expression has to produce a result the compiler will actually check that you don't get to the end with some of the possible values not actually being handled so the compiler will give you a warning if you're missing some and if you if you ignore that warning it'll generate code to throw an exception an exception that says well you ran out at the end of an S which expression you can do that so that's switch expressions tiny little but very neat little syntactic sugar feature if I'm already saying up here that this array is an array of persons why do I have to repeat the type every time in general when I'm in a context where the type is clear why do I have to repeat myself say it's a big generic type of something when I knew up the object can't you just infer that well now we can you don't have to specify these guys you can just when it's given from context that these should be persons you can just write to new expressions like this and then finally default interface members are a feature that helps make it easier to evolve interfaces once you published an interface you're already locked in because if you add another member to it your implementers will break but we're now adding the ability to add members with actually with a method body which is a little unusual for interfaces but it's going to be really useful because now existing implementers like console.log are down here they can get that default implementation without having to change and therefore you've added that member without breaking them of course new implementations will likely provide their own implementation so this is mostly a feature just to make sure that you are compatible that was everything have a great connect thank you very much
Info
Channel: Microsoft Visual Studio
Views: 289,047
Rating: 4.925693 out of 5
Keywords: C#, .NET, Visual Studio
Id: VdC0aoa7ung
Channel Id: undefined
Length: 12min 56sec (776 seconds)
Published: Tue Dec 04 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.