.NET 6 is here and c# 10 is here.
And I'm going to break down some of my favorite features.
So tune in.
Hey everyone.
I'm James and I'm back talking about my favorite C-sharp 10 features.
I love every single version of C sharp.
It just gets better and better and better.
And C-sharp 10 is an amazing release that really helps simplify code and shrink code
to both horizontally and vertically, too.
I'm really excited about it.
And there's some amazing sessions, documentation, and blogs on it, but I
want to break down my favorite features and how to turn on C-sharp 10, if you're
still targeting older versions of.
So let's go and break it down.
All right.
So I'm actually using Dustin Campbell and Mads Torgeson sample here
because it's a really good sample that they've been building on.
I'll put a link to their Donnette conference session, but I'm
just gonna break down some of my favorite features here.
This is a simple application that they've been sort of updating throughout
the years of C-sharp to, I don't know, update things and him break it down
and show off a new features of C-sharp.
So this is really cool.
Actually uses the, the width expression here to make sort of
a deep copy, uh, of the person.
So there's a Scott Hunter, and then it makes a copy of this
other person, Scott Hanselman, and then another copy of it here.
Uh, and then it shows off the equality operators, which is.
And that's really nice because it's using the new record type for person here.
So I think that's really, really neat.
Cool part here is if you are using records, they're already a class,
but now in C-sharp 10, you can just say that it's a class two.
Uh, you can also make it a strapped as well.
So if you really want to make it a record struck.
So if you're worried about Hackford, uh, heat versus stack, or you want
to put it, it's already there.
And the nice part about.
Is that you actually get the equality operators, everything builds
right into, so that's why these equals equals work automatically.
So that's pretty cool.
So if you are working with, uh, records, now you can make them a class.
You can make them a struck 10 it's up to you.
If you want to put them there or not, which is.
Now they also updated not only like records, but also strucks in this one too.
So I've actually just make this a struct.
Uh, most of the things continue to work in this code.
It's a simple, you know, first name, last name here.
Um, but we'll also see that there's, this person equals equals original
person here that no longer works.
And that's because the equality operator is no longer there on a struck.
So you have to implement.
Um, but they, they did make some other enhancements to other
things such as anonymous types.
So for example, if I just say this is a new person, that's going to
continue to work now because they quality operator will work there.
And we can see that the anonymous type is a new string string,
which is kind of cool too.
So there's all sorts of kind of good stuff in there, which
I think is really, really neat.
So strokes, record types, all that good stuff.
Now, one thing that Dustin showed off, which was cool, as
he said, let me just go ahead.
Put this into a new class, so move it to a person dot, um, CS file.
So it's going to do that over there and let's take a look at that.
Does that go over it up a person?
Not CS.
Here it is.
And let me go ahead and maybe put that into a, uh, namespace.
So we'll call it models.
There you go.
Perfect.
Now what's cool about that is, you know, visual studio, 2022, just
kind of deal the refactoring for me.
Uh, but what it did is it brought in the namespaces automatically because
it's doing this file right line, and then we just added a namespace.
So two of my new, new, new, new favorite features I got to say
are, uh, file scoped namespaces.
So this is the ability to take back.
That vertical or horizontal space on our code.
So this is really neat because normally you're not using multiple,
um, you know, things, uh, multiple namespaces in a, uh, in a single file.
So normally you'd have something like this, which is person,
person to maybe this is like, you know, pet or something like that.
And you have these different structures inside of there.
It's really rare to have multiple namespaces.
So what they did is they said, well, what is this.
File.
And you could scope it to a specific namespace such as models.
So you can, all you got to do is do a semi-colon and now you can just
delete and take back your space.
Amazing.
So, so far we've, we've kind of tackled that and you can do that in
any file, which is really, really nice.
And that helps really simplify the.
Now the other ones are about sort of shrinking that vertical space because
here we have like using system IO.
If I go over here, we have, you know, using static system dot council over here.
Um, and.
It's kind of like, why do I need to do all these, you know, using statics?
And if I had a system I own, I had another file on another, file it up to copy and
paste those usings all over the place.
Now, some people like to see their usings and that's totally cool.
I would like to hide the usings.
Um, so what you can do is create a file.
Inside of your project.
So here, this file is just called the usings and this file doesn't do anything.
Um, except for it has all the usings that are in this actual application.
So I'm going to hold down shift alt and then just go down.
I was going to do MultiCare selection.
I'll say global, and these are global usings.
So what I'm saying is, Hey, use all of these usings in the
entirety of the application.
And now if I go back over into a person, for example, we'll see that
it's great out and go ahead and remove that namespace, which is pretty rad.
If I go over it back into my program, we'll see over here that there's
a using static system council.
And that's also great out because you can come in and, um, you know, do static
usings or aliases or anything like that.
Now let's say though, that you wanted to have, have that person back in
here, so let's do person again.
And we wanted to bring in that namespace so we can go ahead and
bring that in right there, right.
Using models.
But again, what if we have a bunch of classes all over the place, but you can
take that using shove it right over here and then put a global in front of it.
The global.
And now models will be brought in as well to the entire application.
And there it goes.
Now, of course, this equality operators not going to work,
so let's make it anonymous one more time, but check this out.
Now, our application starts online.
One, our code inside of the person class, that namespace is online one, and
it's just a beautiful, beautiful thing.
Shrinking that down.
But you're probably saying, well, James, like what about all these,
you know, system namespaces here, what's up with that and why would
I need to bring in all those?
Well, the other feature of C-sharp 10, which is really cool, and part
of.net is sort of mashing the SDKs and.
Global usings together in something called implicit usings.
And this is a cool feature that every SDK and every framework or library
could specify a list of namespaces that are global, because you'd
probably want to use them everywhere, probably like all of these systems.
So you can double tap on your CS proj and you can just come in and
say, um, implicit usings enable.
So you can do enabled true or disable, just like.
And now when I come back over to my usings, I'm gonna actually go ahead and
delete all of the system ones there.
So now, for example, if I needed to do, you know, if I put in using
system.io there, you would no longer need it, which is pretty cool.
I don't need to have that there at all.
In fact, it's, it's just brought in automatically it's are manually
working in there because that is brought in under the hood for us.
They're probably saying James will.
Or my global usings and implicit usings like they must exist somewhere.
Well, let's actually go and do show all files and visual studio.
I'm going to OBJ debug net six and notice that there's a global
using dot G generated file.
These are the auto-generated global usings for the specific project types.
So anything inside of here enables you to basically use those global users.
Oh, in the documentation, there's all the documentation for what's new
in C-sharp 10, but there's also this document on implicit global using
directives new and C-sharp projects.
And you can actually scroll down a little bit and you can notice that
here's Microsoft net, SDK web worker, WinForms WPF, and there's more to, uh,
such as dotnet Maui and other ones.
I mean, you see, if you go to web, it's going to bring in all of these
other ones automatically here for you.
Same with wind phone, a windows forms, WPF and others too,
which I think is super rad.
That's really, really cool.
So you can take a look at all those.
You can turn them on, you can turn them off totally up to you.
I simply love it.
I think it's amazing.
And just regaining that space in your project.
So, for example, if I go to like a web application, this is a new
file, blank asp.net core web app.
I can do the same thing if actually double tap on web application.
You'll see that in file new projects, both nullable and implicit usings are enabled.
And again, if I go down here into the OBJ DB, No six global usings
you'll see here that all of these global usings are brought in for me
automatically, which is really cool.
What I love about this is that I always forget to bring in the dependency
injection using statement, which means I always forget to use all the
extension methods, but now they're there.
And every single file, which I think is absolutely.
Another one quick, really cool one here is that this actual map get just takes
on this delegate, which is kinda cool.
Um, so you can actually come in and you can make that like a bar, um, local,
or I'll just say not local, but I'll say a thing every go, and I can just
make that a, uh, a delegate there.
And then I can just say thing.
And this thing will automatically make it a funk of string
and you can see over here.
So it automatically knows that there's going to be a delegate T result.
This is a new feature that can automatically, you know, figure out your,
um, your functions for you automatically.
If they can figure it out.
Let me take a look at a more complex sample to back in this
project that Mads and Dustin had.
So if I go into this other stuff, Here's a, here's a more advanced one, right?
There's like a, into parsing of strings.
It's a funk of string here.
You always have to define this, but I'm going to say VAR, which is cool.
And you can see it, it figures it out automatically for you.
And this can do it because it knows that it's going to take
in a string and return it.
In other instances here where it's a funk of a bull in an object.
Where it's like, it doesn't know if it's going to return an integer or a string.
You can still do VAR there too.
Um, but it won't be able to compile it because it can't infer the type so that
you can actually use again, you can do this and complete an object before.
And that's going to tell the compiler and show the compiler that it's actually
going to take an, a, a bull, but then return an object automatically.
So lots of really cool things in there.
And again, check out the what's new in C-sharp other things such
as extended property patterns, um, constant string, uh, interpolated
strings, which are really cool.
Um, you already talked about records.
The improved lamb does, um, all sorts of different declarations and, and record
types that you can seal all sorts of little things there, which is really cool.
Now, one of the other things you can do though, is let's say you needed
to enable this an older project.
So here's my MVVM helpers project and here's my old namespace.
And if I go into like observable object, we have all this other stuff here.
You can just go into your CS proj.
And you can see I've already kind of overwritten this, this project is a net
61 net standard, one O and two O projects.
It can kind of be used everywhere.
You can just say Lang version 10 and they'll just enable it.
This would just flip it on.
So Lang version 10, you should be totally good to go.
And now I should be able to actually just come in and visual studio
should give me this little, uh, yeah.
Convert to, uh, file scoped namespace.
And I can just go ahead and do that.
Look at that.
I could also now come in and just flip on, uh, implicit usings navel.
There we go.
And then come back over into my projects and let's give it a little
bit of time to, uh, kick in here.
There we go.
Look at this automatically is bringing some namespaces they're
gone rad, same thing here.
I can, uh, go in and convert those.
Amazing just automatically start removing this code automatically for me.
And that's going to just compile up no problem at all.
Cause it's just compiler, right?
Just it's all you need to really worry about.
So you can go through your application now and then just, you know, do that.
And again, I'm going to now put in a new file over here.
So I'm just going to need a new class.
I don't know.
It doesn't really matter what it is.
I like to call it global usings cause it's literally.
It is.
So I'm going to say global usings and you can put it anywhere technically, but
I'll go ahead and just delete that stuff.
Let me see grouping.
Totally good.
Now, I mean, this is just like, I mean, between these two features, it's
definitely being my favorite features, but now I can just come in here.
I can just now do global, using global, using.
And now, I mean, look at this.
I mean, that's exactly how I want my code to look.
It's astonishing.
I love it.
I've regained so much space in my MVVM helpers class.
It is spectacular.
Check out the documentation for all of the C-sharp 10 features.
Scoped namespace is my favorite.
Um, global usings implicit usings.
I love the new funk stuff.
I love the new stuff happening in records and strikes so much, little
goodness in your application for C-sharp 10 that you can add in there.
So go check it out.
Uh, hope you like this video.
If you do, obviously give it a thumbs up subscribe if you want to, but
also check out Mads and Dustin video.
I'll put it right up over there at this point in the video, and I'll put
it in the show notes as well below.
It's a great breakdown is cool to see Mads and Dustin, um, breaking down
all the different C-sharp features.
This is my mini recap of theirs, but I also wanted to show how to
flip it on for older projects too.
And it was, I hope you enjoyed this video.
If you did give it a thumbs up.
Super appreciate it.
Let me know your favorite C-sharp feature is down in the
comments below until next time.
Thanks for watching.