[00:00:01] [music] [00:00:06] >> And there we are. [00:00:07] >> Hello, hello. [00:00:08] >> Hey. Uh, thanks for um an amazing [00:00:11] keynote. Um, I'm here now with uh [00:00:13] Dustin Campbell. [00:00:15] >> And I'm here with Mads Torgersen. [00:00:16] >> Yeah. We both work on C Sharp. [00:00:18] >> We do. [00:00:18] >> And we all we can talk about is C Sharp. [00:00:20] And here we are doing it again. [00:00:22] >> What version is this one? [00:00:23] >> 15. [00:00:24] >> 15. Okay. [00:00:25] >> It's the .NET version plus four. We've [00:00:27] been following that for a while. [00:00:28] >> Yeah, okay, okay. I I didn't know the [00:00:30] formula that works. [00:00:31] >> Yeah. [00:00:32] >> All right. Well, you want to get to it? [00:00:33] >> Um, let's just uh let's get into it. [00:00:36] >> All right, let's dive in. [00:00:37] Okay, so [00:00:39] let's make sure we can see things. [00:00:42] Can we see things? [00:00:44] >> Can we see things? [00:00:46] >> Is there uh [00:00:47] >> We can't see them in here. [00:00:48] >> There we go. Okay. [00:00:50] >> Okay. [00:00:51] >> So [00:00:52] you showed something very similar to [00:00:53] this or possibly identical to this in [00:00:56] the keynote, but it's where we want to [00:00:57] start, right? [00:00:58] >> Yeah, no, and I I was in light mode, so [00:01:00] this is completely different. [00:01:01] >> Okay, yeah. Totally different. This is [00:01:03] the the darker, more nefarious version [00:01:05] of the demo. [00:01:06] >> Yes. So unions, okay. Um, [00:01:09] so long we've been talking about adding [00:01:11] unions to C Sharp. [clears throat] Now [00:01:14] as of today, we finally have them. [00:01:15] >> Excellent. [00:01:16] Um, you know, and this is this is [00:01:18] something that [clears throat] [00:01:20] it's another one of these features that [00:01:21] again when I joined Microsoft now like [00:01:24] 18 years ago or something like that, we [00:01:26] were we were kind of talking about it [00:01:28] then. So uh you know, it's it's been a [00:01:30] long time. A lot of designs came through [00:01:32] and I'm really excited about this one. [00:01:34] >> Yeah. [00:01:35] And so let's take Yeah, this is the same [00:01:36] example as I showed, but we'll go into [00:01:38] more detail this time. [00:01:39] >> Yeah. [00:01:39] >> Um, and uh we have this situation where [00:01:43] we want to work with a a certain set of [00:01:46] types. [00:01:47] >> We have we have four types here. We have [00:01:48] dog, cat, bird, shark. But the idea is [00:01:51] that these come from lots of places, [00:01:53] right? We don't own This isn't a [00:01:55] hierarchy that we have built ourselves. [00:01:57] We're pulling it from other sources, [00:01:59] other libraries, um whatever, and kind [00:02:01] of want to use them, right? [00:02:02] >> Yeah. So we, yeah. Um, and using them [00:02:05] together, up until now, you don't really [00:02:07] have a lot of options. This is This is [00:02:08] the easiest thing you can do, um which [00:02:11] is to use their common base type to hold [00:02:13] them. [00:02:14] >> object all the way to object, just like [00:02:15] .NET 1.0 except with generics, so you [00:02:17] know, a little bonus. [00:02:18] >> Yeah, yeah, you know, .NET 2. [00:02:20] >> Yeah. [00:02:20] >> But um but still, uh right? So uh [00:02:23] obviously things can sneak in there. Uh [00:02:25] we put the shark there just to make it [00:02:27] sound really scary. Some people do have [00:02:29] sharks as pets, but [00:02:31] um [00:02:31] >> We're making a statement against that [00:02:32] today. [00:02:32] >> this demo we are We're just like that's [00:02:34] not for us. Um [00:02:36] but one snuck in there anyway, right? [00:02:38] Because we don't have any type safety [00:02:40] around that. [00:02:41] >> No, we don't. And uh yeah, so so the [00:02:43] next thing we want to do to try and like [00:02:46] get some handle on this, if we don't [00:02:47] think sharks should be sneaking in [00:02:49] there, we need some way to to kind of be [00:02:51] able to talk to about these types as a [00:02:53] group, as a set of types, right? So and [00:02:56] that's what unions are. [00:02:57] >> That's what unions are. And we should [00:02:59] probably call out before we go [00:03:00] >> Oh yeah. [00:03:01] >> in the um in the consumption of these [00:03:03] types as well, right? We do um like [00:03:05] first of all, yes, you can It's nice in [00:03:07] a switch expression like that maybe that [00:03:10] you can deal with the sharks that sneak [00:03:11] in there, but this never ends, right? [00:03:13] There's a warning and an exhaustiveness [00:03:15] warning. [00:03:15] >> It's a problem with having object is [00:03:17] that it really is the set of all things [00:03:19] possible in .NET, right? [00:03:20] >> please handle all things possible? And [00:03:22] there's no good way of doing it. [00:03:23] >> except for that that pattern it suggests [00:03:25] there that yeah [00:03:26] >> So there is a way of doing it, but not a [00:03:28] good way, right? Because now you are [00:03:29] just silencing everything that might [00:03:31] come in. [00:03:31] >> Yes. [00:03:32] >> Um the best thing we could do is [00:03:33] probably throw. Um but we can do better [00:03:36] with unions. [00:03:37] >> Yes. [00:03:38] So the union uh when you declare a [00:03:40] union, it's using the union keyword. [00:03:43] Surprise. [00:03:43] >> It's a new type declaration. [00:03:45] >> it's a new type declaration. So call it [00:03:47] pet. And we want dogs, we want cats, we [00:03:50] want birds, and we do not [00:03:52] do not [snorts] want sharks, right? [00:03:54] >> So this is So um this is the syntax. [00:03:57] It's Pet is a union of dog, cat, bird. [00:04:01] That's all there is to it. We call those [00:04:03] case types. Um, and that's it. [00:04:05] >> And now we can change our list of object [00:04:07] to a list of pet, and now we get key [00:04:10] errors in our code. The first is that [00:04:12] Shark cannot implicitly convert to pet, [00:04:15] which is [00:04:16] is interesting in itself. By creating [00:04:18] the union, we're [00:04:19] we're saying that like we're allowing [00:04:21] these types, these case types are [00:04:23] implicitly convertible to the union [00:04:25] type, which is interesting. Um, so [00:04:27] that's an interesting error. And then [00:04:29] down here we get an error because we're [00:04:31] switching on a pet which is of the union [00:04:35] type pet, and Shark is not a case class, [00:04:37] so it can't handle that pattern, right? [00:04:39] >> Right. So so on the positive side, the [00:04:42] um the union gives you implicit [00:04:45] conversions from case type to union, and [00:04:46] it lets you pattern match uh [00:04:50] on the union type against the case [00:04:51] types. [00:04:52] >> Yeah, and and regular pattern matching. [00:04:53] I mean, we're pulling out, you know, [00:04:54] values out of these from their from [00:04:56] their data um as they were defined. Now [00:04:59] so I can delete these guys because [00:05:01] they're errors, and we don't want Sharks [00:05:03] in our pets. Um, [00:05:04] >> All for more safety there. [00:05:06] >> And then the the really critical thing, [00:05:08] I think, and the most instructive thing [00:05:10] is that the warning is gone now, [00:05:12] >> Yeah. [00:05:12] >> right? Because we have a set of exactly [00:05:15] three things, and this pattern match, [00:05:17] this switch expression matches all three [00:05:20] things. It's exhaustive. [00:05:22] >> Yeah. [00:05:22] >> So um yeah. [00:05:24] >> Yeah. So that is really great. And maybe [00:05:26] um like so we we put some types down [00:05:29] there. What if what if you put a [00:05:30] nullable type instance? What would you [00:05:33] >> uh we could say cats are nullable. I [00:05:34] mean, I've heard there's a there's a [00:05:36] scientific possibility for that sort of [00:05:38] stuff. [00:05:38] >> to Schrodinger's cat and yeah. [00:05:41] >> This is the less dire version. Even [00:05:42] though in dark mode, we are not actually [00:05:44] killing the cat. [00:05:44] >> No, no. Uh we just say that it won't [00:05:46] exist cuz it'll be null. Um, the uh the [00:05:50] this now we're not exhaustive anymore, [00:05:52] but that's actually expected because we [00:05:54] need to handle null. [00:05:55] >> We need to handle null. [00:05:56] >> Right. So we can come in here, we can [00:05:57] say, okay, let's handle null. [00:05:59] Um, [00:06:00] and uh let's give it a nice little I [00:06:02] don't know, [00:06:03] little [00:06:04] little cat. [00:06:05] >> Yeah. The cat has [00:06:06] >> flown. [00:06:06] >> You know, I'm going to make him a [00:06:07] pouting cat. [00:06:09] He's not real pleased about this [00:06:10] situation. There we go. [00:06:12] >> Yeah. Yeah, so um [00:06:14] >> And now we're exhausted. [00:06:15] >> Now we're exhausted again. And the [00:06:16] warning was really specific about, oh, [00:06:18] you're not handling null. It tells you [00:06:20] what to deal with. So um [00:06:23] again, this is very um [00:06:25] the the the compiler feedback [00:06:28] that you get is is very helpful, very [00:06:30] constructive. It takes you to the next [00:06:32] step. [00:06:33] >> Yeah. [00:06:33] >> And when the warning's gone, you know [00:06:34] you've dealt with everything. [00:06:36] >> Now as we've been writing this kind of [00:06:37] short form of union, it's very different [00:06:39] from like when you usually when you're [00:06:40] writing like a class or something. Like [00:06:42] a like it's like a positional record, [00:06:44] but it it's it's pretty it's pretty [00:06:46] succinct, but it is really a type [00:06:48] definition. [00:06:48] >> It is. Yeah. [00:06:49] >> Right? So I could add you know, I could [00:06:51] add generic type parameters and use them [00:06:53] or um but also I mean we have a [00:06:56] semicolon there, but we could very [00:06:57] easily just put braces as if it were any [00:07:00] sort of type because it is. [00:07:02] >> Yeah. [00:07:02] >> And then I could take and give it [00:07:04] members. [00:07:05] Right? So we can come down here, grab [00:07:07] this and we could say, you know what, [00:07:09] that's that's silly to do that up there. [00:07:11] Let's uh [00:07:13] let's make it a property on here. [00:07:15] >> Yeah. Uh little helper property. [00:07:17] >> Yeah. [00:07:18] >> Um [00:07:18] >> And we're going to say we're going to [00:07:19] switch on this, right? [00:07:21] >> Yeah, cuz now you're inside of the union [00:07:22] itself. You just get to switch on it and [00:07:25] do things depending on what's inside. [00:07:28] >> Yeah. So now we can get rid of that [00:07:30] and [00:07:31] yeah. There we go. [00:07:33] >> Yeah, so um you can put any kind of [00:07:36] function member in there. We don't [00:07:37] actually let you uh declare state [00:07:40] additional state in there. We're saying [00:07:42] that [00:07:42] the value inside is the state and you [00:07:44] don't get to declare other state like [00:07:46] because that would probably be silly. [00:07:48] Um so that's mostly like to protect you [00:07:50] from doing something. [00:07:51] >> When it's declared as a as this union [00:07:54] key, yeah. [00:07:54] >> yeah exactly. So um so function members, [00:07:57] any helper members you like. Uh the list [00:08:00] of case types up there can really be any [00:08:02] type that is convertible to object. Um [00:08:05] >> Yeah, absolutely. You could you could [00:08:06] put [00:08:06] >> type parameters, interfaces. [00:08:08] >> I could put in bool, I could put in [00:08:10] whatever, right? [00:08:10] >> And we don't care if they overlap or [00:08:12] anything, like that's your problem. Um, [00:08:14] if you want to overlap types in there, [00:08:16] um, it's going to the contents are going [00:08:18] to answer a yes to either of those types [00:08:21] when you pattern match. So, yeah, it's [00:08:23] it's fully kind of free for for you to [00:08:26] to use in any way you like. [00:08:27] >> So it's pretty cool, [00:08:28] but um, so often in C Sharp when we [00:08:31] build up these features, we're building [00:08:32] them up out of other things, right? [00:08:35] We're not [00:08:36] adding brand new conceptual uh, types to [00:08:40] the CLR or any of that kind of thing, to [00:08:41] the runtime. Um, so this is this is the [00:08:44] way the you know, with the default kind [00:08:47] of kind of union implementation the [00:08:49] compiler implements, what what does that [00:08:51] actually look like? [00:08:52] >> Yeah. [00:08:52] >> And what I mean I'm just I'm very [00:08:54] curious about that. [00:08:54] >> no but we can actually show that because [00:08:56] uh, the compiler is making some choices [00:08:58] here. How is the value stored inside and [00:09:01] and surfaced back out uh, [00:09:03] during pattern matching and so on. What [00:09:04] is this actually? [00:09:06] Um, and you may or may not like those [00:09:08] choices. You can declare your own union [00:09:11] manually and make those choices yourself [00:09:12] if you want. And we let's just go and do [00:09:14] that, make the same choices as the [00:09:15] compiler but make them explicit so you [00:09:17] can see how that all works. [00:09:18] >> yeah, in fact it is a struct, right? By [00:09:20] default that's that's what they are. And [00:09:22] so, well, now this is not exactly legal, [00:09:24] so get rid of that. [00:09:25] >> Yeah. [00:09:26] >> Okay, and now everything's broken. [00:09:28] >> Everything's broken [00:09:29] uh, because this is [00:09:31] this this is not a union, but we can say [00:09:34] you can say any class or struct type, [00:09:35] you can say it's a union just by putting [00:09:37] an attribute on it saying [00:09:39] >> It is aptly named union. [00:09:41] >> Yeah. Yeah, we didn't get too fancy with [00:09:44] that. Yeah, yeah, you should be able to [00:09:45] remember that. So now a little bit of [00:09:48] the red disappeared because now it is a [00:09:49] union type, but [00:09:50] >> I think we and we got a another piece of [00:09:51] red, which is right here [00:09:53] on pet itself, but it tells us, again [00:09:55] the compiler guides us to like what [00:09:57] needs to be added to make this a proper [00:09:59] union. What's missing that makes this a [00:10:01] real union? Um, the the the first thing [00:10:04] we'll get to, but the second thing is [00:10:05] about we need a value property, which is [00:10:07] the place where it gets the thing that's [00:10:10] that is actually is in that moment, [00:10:12] right? The piece of data. [00:10:14] >> Yeah. The compiler needs to be able to [00:10:15] ask it, what value are you actually? [00:10:18] >> And so that's just we'll just make that [00:10:19] a nullable value. And what I love about [00:10:21] making a nullable value, it can be it [00:10:23] can be it can be nullable object or it [00:10:25] can be, you know, as a nullable [00:10:26] reference type or just regular object. [00:10:28] But when we declare a nullable, we know [00:10:29] that, you know, hey, we're we're [00:10:30] handling null here. And so that the [00:10:32] error went away from null cuz that case [00:10:34] is fine. It just doesn't know anything [00:10:36] about dogs, cats, and birds. [00:10:37] >> it doesn't know which uh case types it [00:10:39] has. And instead of having like some [00:10:40] fancy attribute listing them or [00:10:42] something, it's much simpler. It goes to [00:10:44] the other error that you get when you [00:10:46] hover on the um [00:10:48] on the pet there. [00:10:49] >> Mhm. [00:10:50] >> Um like if you hover up on the on the [00:10:52] declaration, [00:10:52] >> On the declaration [00:10:53] >> says a union type must have at least one [00:10:56] union creation member. And um excuse the [00:10:59] fancy language, but in in most cases [00:11:02] we're just talking about constructors. [00:11:03] >> Yeah, so we're just going to [00:11:04] >> constructor. [00:11:05] >> We're just going to declare a [00:11:05] constructor and have it take one of [00:11:08] those case types. [00:11:09] >> Yes. [00:11:09] >> And we'll just assign it to that value [00:11:11] uh property. And now [00:11:13] >> Oh, look what happened. [00:11:14] >> Well, dog is now considered a case [00:11:16] class, a case of pet. So um we can [00:11:19] pattern match to it. Um we can we're [00:11:22] implicitly convertible to pet. [00:11:23] >> Yeah. [00:11:23] >> And if we add constructors for [00:11:25] everything else and we don't need to [00:11:26] declare nullable here, um you know, that [00:11:29] that is handled [00:11:30] >> Yeah. [00:11:31] >> you know, elsewhere, but we could. Um [00:11:33] and we can say [00:11:35] there we go. [00:11:36] Here we go. [00:11:37] >> Yeah. [00:11:38] And now we have the case types and [00:11:40] because we express the case types in [00:11:43] terms of constructors, that right away [00:11:45] that tells you how the compiler [00:11:46] implements that implicit conversion that [00:11:49] happens up top, right? Because um it [00:11:51] just when it's it says when it when it [00:11:54] gets a dog, it wants to turn it into a [00:11:56] pet, it just calls the constructor. [00:11:58] >> Yeah. [00:11:58] >> That, you know, that's it. And when it [00:12:00] does pattern matching, it uh calls that [00:12:03] weekly type value property uh to get the [00:12:05] value out and just applies the pen to [00:12:06] that. [00:12:07] >> Now there's one thing missing here that [00:12:09] we do the compiler does add. So if we [00:12:11] want to have like a representation of [00:12:13] what the compiler generates when you use [00:12:15] the union keyword, that's to add [00:12:17] uh an implementation of the I Union [00:12:19] interface, okay, which is literally just [00:12:22] the value property, right? It's just [00:12:24] there so that now at runtime you can [00:12:28] talk about types or objects that you get [00:12:31] and see if they are unions and get their [00:12:32] values out of them uh or constrain [00:12:34] against them with generics or anything [00:12:36] like that. [00:12:36] >> Yeah. So you get to work If unions [00:12:38] implement this, you get to work with [00:12:40] them without knowing the exact union [00:12:42] type and L level thing. [00:12:44] >> but it is you know it's useful. [00:12:45] >> The compiler doesn't care about it, but [00:12:46] it puts it there when it generates [00:12:48] unions. [00:12:48] >> Yeah. [00:12:49] >> And this now is exactly what the [00:12:51] compiler generates when you use that [00:12:52] union syntax from before. [00:12:54] >> It's a lot more cumbersome to write it [00:12:55] that way. [00:12:56] >> It is. So why would you ever do it? Um [00:12:59] well maybe [00:13:00] there are a couple of main cases I [00:13:02] think. One is [00:13:03] >> Uh-huh. [00:13:04] >> Haha. [00:13:05] Um so it's the union of two I guess. Um [00:13:08] uh there One is that you see that [00:13:11] there's because of the weekly typed um [00:13:13] auto property in there, if you have a [00:13:15] lot of value types, they get boxed when [00:13:17] they get stored in um in a standard [00:13:18] union. If you don't don't like that for [00:13:20] performance purposes, you can write your [00:13:22] own that does its own storage let that [00:13:25] doesn't box. Um and then there are some [00:13:27] extra members that that we let you [00:13:29] declare that the compiler will prefer [00:13:31] >> Mhm. [00:13:31] >> for getting at the at the values in a [00:13:34] strongly typed way so it doesn't box on [00:13:36] the way out, you know, either way. [00:13:37] >> And so if you have a highly efficient [00:13:39] way you want to store things, if you [00:13:41] want you have or you have an existing [00:13:43] union-like type that you want to make [00:13:46] look like a union, there are uh there [00:13:49] are avenues for you. There are um This [00:13:51] is all documented on the on the uh [00:13:53] learn.microsoft, but there's all sorts [00:13:55] of ways to kind of advanced ways to kind [00:13:58] of tweak the union types and the [00:14:00] compiler will respect all of those. [00:14:02] >> Yeah. Take that existing type you have, [00:14:05] start with putting the union attribute [00:14:06] on it and then we we have patterns and [00:14:09] patterns with patterns to that the [00:14:11] compiler will recognize so you can get [00:14:13] the behavior that you [00:14:15] >> Yeah. [00:14:16] Cool. [00:14:17] So, that's unions. [00:14:18] >> That's that's a I guess all there is to [00:14:20] say about unions, right? [00:14:21] >> Yeah. I think that it is. [00:14:22] >> Um, [00:14:23] hope you like it. [00:14:24] >> We have some other things though. [00:14:25] >> We do. [00:14:26] >> The C Sharp 15 is not just unions, [00:14:28] though [00:14:29] it it's a lot of it is unions. [00:14:31] >> Yeah. [00:14:31] >> Um, and so I've moved over to another [00:14:34] piece of code which is um you know we [00:14:36] were trying to think of a demo for this [00:14:37] and we're like well we're just we're [00:14:38] just computer science guys, right? So [00:14:40] let's just make a calculator. This looks [00:14:42] like our homework. [00:14:42] >> Yeah. We [00:14:43] >> Um you know we've got operations and [00:14:45] there's a that's an abstract base class [00:14:47] with number, add, subtract, multiply, [00:14:49] divide and and a compute method that [00:14:52] just takes those and it calls itself [00:14:54] recursively and performs the uh the [00:14:57] operation. So you know very much a Mad [00:14:59] and Dustin sort of homework uh problem. [00:15:01] >> And the compute method, you know instead [00:15:03] of being a virtual method in the [00:15:05] hierarchy like you sometimes do, [00:15:06] >> Mhm. [00:15:07] >> you know it's on the outside because you [00:15:09] can that with using pattern matching cuz [00:15:12] that's the way that you can add [00:15:13] more functionality, more different [00:15:16] functions like a a pretty print or [00:15:17] whatever um [00:15:19] outside of the model. [00:15:20] >> programming style this would be how I'd [00:15:21] do it, right? Yeah. [00:15:22] >> Yeah. Yeah. So that's all great except [00:15:24] for one little thing which we've seen [00:15:26] with unions as well. There's one little [00:15:28] fly in the ointment. [00:15:29] >> It's uh that again that that it there [00:15:32] could be anything that is uh [00:15:36] inheriting from operation. And so how [00:15:38] can we possibly handle all of them [00:15:40] without the discard? [00:15:41] >> But this is so annoying Dustin cuz we [00:15:42] are declaring all of them down there and [00:15:44] that is those are the only ones we want. [00:15:45] How can there be how can there be other [00:15:47] Yeah. [00:15:48] >> It's a library. And and so we have an [00:15:49] abstract base class that's public. So [00:15:51] anybody can go and you know make things. [00:15:53] In fact if I were to like do like go to [00:15:55] implementation on this um I could find [00:15:58] in here that that in fact somebody has [00:15:59] done that and that there is an evil add [00:16:02] and an evil subtract method in some [00:16:03] other project some what so I know. [00:16:06] >> We have to thwart this. [00:16:07] >> Yes. Uh we we must uh this is we must [00:16:10] stamp this out. [00:16:11] >> So first idea that comes to mind now [00:16:13] that we just did unions is can we just [00:16:15] do this with unions again? [00:16:16] >> You could. [00:16:17] >> Um yeah we could definitely So, we could [00:16:19] make a union of those five derived [00:16:23] classes and then publish that one and [00:16:27] uh that would be another that we call it [00:16:29] op two or something like that. [00:16:31] >> But we'd have to change the functions, [00:16:32] right? We'd have to change compute now [00:16:33] to take an op and all that kind of [00:16:35] thing. And that's [00:16:36] >> and recursively they don't contain other [00:16:39] ops and you'd have to wrap them and it's [00:16:40] this this is painful. [00:16:42] >> Yeah. [00:16:42] >> Um, that's not the scenario that unions [00:16:45] were created for. But we still would [00:16:47] like to somehow close off that class, [00:16:49] right? Cuz if we could prevent others [00:16:50] from inheriting from it, [00:16:53] our exhaustiveness check could trust [00:16:55] that these like our own derived classes [00:16:57] from it are the only ones. [00:16:58] >> though they're public, right? [00:16:59] >> though they're public. [00:17:00] >> Yeah. So so you said to close them off. [00:17:03] So if I were to like write that word [00:17:05] close Oh, hey, there's something in in [00:17:07] the completion list there. [00:17:08] >> Oh. [00:17:10] >> Oh, look at that. [00:17:11] >> Oh, look at that. [00:17:11] >> The warning went away. [00:17:12] >> That that worked. Um, so yeah, you can [00:17:16] you can close a class with the closed [00:17:17] keyword. That makes it automatically [00:17:19] abstract. [00:17:20] >> Yeah. [00:17:21] >> Uh, which is why you replaced that [00:17:22] keyword. [00:17:22] >> Yes. [00:17:22] >> And then somehow magically the uh [00:17:25] exhaustiveness uh warning went away. I [00:17:28] wonder what happened to our evil doers. [00:17:30] >> Oh, look at that. The evil doers are now [00:17:32] in fact [00:17:34] have errors. [00:17:35] >> Because [00:17:36] >> They're illegal. [00:17:37] >> Because they're in a different assembly, [00:17:39] a different project if you will. And [00:17:41] they don't get to inherit. When you are [00:17:43] a closed class, only [00:17:45] in your own assembly do you get to [00:17:46] directly inherit uh [00:17:49] from that. So [00:17:51] that nicely gives us an exhaustiveness [00:17:53] guarantee once again, just like with [00:17:55] unions, but with different means, right? [00:17:57] And this this again I I think is a very [00:18:00] common situation. You build a nice data [00:18:02] model with inheritance. [00:18:04] >> Pretty sure I ran into it with C Sharp [00:18:05] 1.0. [00:18:06] >> yeah. Can't keep others out of it, you [00:18:08] know? And I'm like I want to I want to [00:18:10] close down the set of things this can be [00:18:12] and there are tricks you can do to [00:18:13] prevent others from doing this, nasty [00:18:15] tricks with constructors or what not. [00:18:17] But they don't give you the [00:18:18] exhaustiveness guarantee here now [00:18:20] though, [00:18:21] the exhaust exhaustiveness warning is [00:18:23] gone because the compiler knows. [00:18:25] >> That's fantastic. I'm excited about this [00:18:28] one. [00:18:28] >> Yeah. [00:18:28] >> Um [00:18:29] we have a couple more to show though. [00:18:31] >> Let's do it. [00:18:31] >> Okay. Okay. [00:18:33] So first up uh this is something that um [00:18:37] you know I've wanted for a while. [00:18:38] Personally I love collection [00:18:40] expressions. [00:18:41] >> Me too. [00:18:41] >> Remind me again, was it the C Sharp 12 [00:18:44] 10 I don't [00:18:45] >> have looked it up. [00:18:45] >> I don't even know anymore. It's but it's [00:18:47] been you know but they're so useful so I [00:18:49] mean they become kind of ubiquitous. Um [00:18:52] they're a great way to just like declare [00:18:54] a you know a piece of just a collection [00:18:56] and you know if I'm passing it to a [00:18:58] method I can change that method's type [00:19:00] and it probably it'll continue working. [00:19:02] Um just adore these. But when I want to [00:19:05] get like you know specific about [00:19:08] the how that collection is constructed [00:19:10] um that's where things get [00:19:13] tough right now right? You have to now [00:19:14] you can't you got to fall back to like [00:19:16] okay if I want to pass for example I [00:19:18] have on screen I have a hash set and I [00:19:19] want to pass in a comparer so this is [00:19:21] ignore case. Well I need to knew that up [00:19:23] to call the right constructor or if I [00:19:25] want to you know if I'm creating a list [00:19:28] and I want to pass a capacity and I'm [00:19:29] going to add things later or something [00:19:31] like that. [00:19:32] >> Yeah so we fall off the cliff on a few [00:19:34] occasions for that kind of reason. Oh we [00:19:36] did cuz I noticed there's a little [00:19:39] smudge a little [00:19:41] >> little dots here. [00:19:42] We used to call this smudge based [00:19:43] programming right? If I if I uh if I [00:19:45] control dot on that I in fact get [00:19:49] a fix to turn that into something [00:19:51] radically different. [00:19:52] >> Into a collection expression. [00:19:53] >> Yeah it's still a collection expression [00:19:55] but it has this thing in front. Let's [00:19:56] let's go ahead and do that. [00:19:58] >> Yeah so this is a new thing. If you want [00:20:01] to pass [00:20:03] uh arguments to the creation whatever [00:20:05] that is under the hood of a collection [00:20:07] expression you can take up that first [00:20:09] spot inside of the square brackets use [00:20:11] the with keyword [00:20:13] and then give an a list of arguments [00:20:15] that that uh collection type [00:20:18] responds to [00:20:19] um like in the case of hash set um it [00:20:22] likes getting um [00:20:24] compares. So, um we're giving it one. [00:20:26] >> That's fantastic. And so, this is [00:20:28] already just [00:20:30] my code is strewn with it. I've got [00:20:32] Copilot is trained. We're just Yeah, [00:20:33] it's going nuts. So, [00:20:35] >> Yeah. [00:20:35] >> um let's take a look at another one. [00:20:37] This one is kind of one that I I kind of [00:20:38] didn't get it first until I remembered [00:20:40] that I've written this code before where [00:20:42] I've got some outer loop, some inner [00:20:44] loop, and I have a condition in the [00:20:46] inner loop that I want to break out of [00:20:47] the whole thing. So, I've got to like [00:20:48] Oh, let's declare this bit of state. [00:20:50] We'll say if I'm done yet, and set it to [00:20:52] true, and check outside. And you know, [00:20:54] the JIT probably elides all this [00:20:55] beautifully. I haven't looked, but I I'm [00:20:57] guessing. Um, I hope so. But it's like [00:20:59] But it's my eyes have not been elided. I [00:21:02] I'm like I'm like [00:21:04] I hate this sort of thing. [00:21:05] >> it's so easy to mess it up when you when [00:21:06] you're [00:21:07] in there editing the code. [00:21:08] >> Yeah. Yeah. So, um so, new feature in C# [00:21:12] 15, which is the ability to have labeled [00:21:15] breaks and continues, right? So, you [00:21:16] could put a label on the outer loop, and [00:21:19] you could say I'm going to break to [00:21:20] that, right? Um, and again, there's [00:21:23] another smudge, right? There [00:21:25] There is. And we could say use this [00:21:26] labeled jump statement, and it just does [00:21:28] it for us. Okay? So, here's That's a [00:21:30] lot. Here's what it does, [00:21:33] >> right? [00:21:33] Yeah. I'll just clean this up a little [00:21:35] bit. I'll just call it outer. And uh [00:21:37] >> Yeah. So, introduce a label on the on [00:21:40] whichever [00:21:41] uh looping statement you want to later [00:21:44] be able to continue or break [00:21:45] >> Yeah. [00:21:45] >> to. And then you can do it from deep [00:21:47] within, use a name, bypasses everything [00:21:49] in between and goes does it for that [00:21:52] particular one. [00:21:52] >> This is gorgeous. Or you can continue [00:21:54] the outer one, which uh it's that's all [00:21:56] It's beautiful. [00:21:56] >> Yeah. Yeah. So, um less You know, it's [00:22:00] just safer code again. And um yeah, [00:22:03] straightforward. [00:22:04] >> Easier to not make a mistake. [00:22:05] >> And to be fair, many many languages have [00:22:07] something like this. It just kind of [00:22:09] relate to the game. But um now we're in [00:22:11] the game. [00:22:11] >> Yeah. Fantastic. [00:22:13] All right. [00:22:13] >> Let's um [00:22:15] >> We have a couple other thing One more [00:22:16] thing in slides, right? [00:22:17] >> We have one more thing. Let's just show [00:22:18] that one in slides just um yeah. [00:22:20] >> Yeah. Okay. So, back to slides. [00:22:23] And uh this is the other thing, right? [00:22:25] >> This is [00:22:25] >> the other thing. So, [00:22:26] >> um [00:22:27] >> last C# 14 we [00:22:30] was in we we added an even older [00:22:35] requested feature probably than unions, [00:22:37] which was extension everything, [00:22:40] extension members that weren't extension [00:22:42] methods. [00:22:44] It wasn't quite everything though. It [00:22:46] was extension [00:22:47] a lot of useful member kinds including, [00:22:51] as you can see in here, extension [00:22:53] properties. So, the count probably there [00:22:56] is an example of that. [00:22:57] >> That's that was their last time, but it [00:22:58] just putting it there to show that that [00:23:00] that's there. [00:23:01] >> And we said, okay, we are adding the [00:23:02] most useful member kinds now and then [00:23:04] we're going to see as people request [00:23:07] other ones and you know, if we can if [00:23:10] they are sort of straightforward to [00:23:12] make, we'll keep adding ones and [00:23:14] and sure enough in C# 15 we are indeed [00:23:18] adding extension indexers. [00:23:20] >> Yeah. I think the the cool thing about [00:23:22] this is that, you know, as we were [00:23:25] designing this for a long while, but [00:23:27] like and with a lot of ideas, but but [00:23:29] this turned out to be what we said was [00:23:31] the syntax that scales to other members, [00:23:33] right? We had extension methods and they [00:23:35] didn't scale. That syntax couldn't scale [00:23:37] to other kinds and this one does. [00:23:39] >> Yeah, having that [00:23:40] extension group there essentially that [00:23:43] where it says extension of T and tells [00:23:45] you what the receiver is and everything [00:23:46] in one go and then you can just use the [00:23:48] regular member syntax inside. That was [00:23:51] kind of like the key that unlocked all [00:23:52] of this. And and here we are just using [00:23:55] regular indexer declaration syntax [00:23:58] inside and [00:24:01] and that's all it takes. [00:24:02] >> That's fantastic. [00:24:03] So, to sum up on the features we have, [00:24:06] there are so we we we kind of showed the [00:24:08] first the first five here. [00:24:10] >> The first five. The last one [00:24:12] we're going to [00:24:13] let other people show. It's a feature [00:24:16] that is in preview in C# [00:24:18] that we worked with the runtime team on [00:24:21] to increase [00:24:23] the usefulness of the unsafe feature in [00:24:27] C# to where it actually [00:24:30] can give you real guarantees and where [00:24:31] you can sort of track it [00:24:33] transitively through the code base and [00:24:35] ensure that [00:24:37] you can mark your own unsafe operations [00:24:38] unsafe instead of just built-in ones. [00:24:40] That's a really cool thing for [00:24:42] >> Yeah. [00:24:44] >> for [00:24:45] ensuring that your unsafe code is in is [00:24:48] in fact documented and tracked all the [00:24:50] way through. So [00:24:53] that's there as well and you know one [00:24:55] final thing async is faster. It's not [00:24:58] different. It's just faster if you turn [00:25:00] it on. [00:25:01] Cuz the runtime takes care of that as [00:25:03] well. [00:25:04] That's [00:25:05] So that's it for C# 15. [00:25:09] Want to throw it over to Maddie.