There has been a lot of hype around primary constructors in C# 12.
I'm real excited about this feature because after I tried it in not only
some of my smaller applications, but bigger applications, it saved me a
lot of keystrokes, and I think you're gonna absolutely love this feature.
So what I wanna do today is get right into the code and explain what primary
constructors are, how you can use them, and why you're gonna love them.
So tune in.
All right.
I'm just gonna get right into the code and we're gonna talk
about primary constructors.
Let's first talk about constructors.
All right, so I have a very simple class here.
I have a person class here.
I'm creating a new logger, a person that's me, and I'm gonna print
my name and pass in that logger.
Now this person is gonna look like any other data structure in
C# that you've ever seen Here.
I have a public constructor that takes in a first name and a last name.
I have two, uh, little properties here with getters and setter
so I can get them and set them.
Um, in here, I'm setting the first name and the last name.
Now, of course you might be doing other things like modifying that first name
and last name because there's sets and gets and all that other stuff here.
But in this case, what I'm gonna do is concatenate those two properties, the
first name and the last name property, and then over here when I want to
print the name, I'm gonna pass it the logger and I'm gonna log it out.
Now the logger implementation is very simple.
I put it right into this code and I'll put it up on GitHub as well.
Now, what I'm gonna do here is create a logger where I log the message
and it does a council right line.
Very, very straightforward.
Nothing crazy.
Okay, so when I run this application, what we're gonna see is that I should
get my name output here into the council.
So, There we go, James Montemagno.
Not crazy, but if we look at this, it's quite a lot of code, right?
We have this constructor, we have these, um, constructor
parameters being passed in.
They're setting them to the first name.
Uh, over here, you know, we have these public properties, but we're not really
using them a anywhere inside of our code that we can kind of see, right?
We just have them here.
The first thing that we could probably wanna get rid of is just this
constructor that is kind of just a bunch of lines of code for no reason.
They're just assigning those properties.
So let's use primary constructors.
Primary constructors let you shift that constructor up as a primary constructor
for the class or structure, uh, up into the public class definition.
So we're gonna turn on the feature.
All I gotta do is turn on Lang version preview.
So if you have either latest version of Visual Studio or.net eight
install here, I have it Dotnet seven project and it'll work just
fine cause it's compiled language.
Uh, here I can shift it up.
All right, it's on.
So I'm gonna take this instructor, take it and move it up.
Then what I'm gonna do is I'm gonna delete that primary const
or that actual constructor there.
All right.
Now that's very fascinating.
I haven't changed the rest of my code.
Nothing else is going on here, but.
I'm basically move those constructive parameters up into this declaration.
So we've declared these types in the, uh, inside of here.
Now these constructor parameters of first name or last name aren't used
yet, so we can use them in a few ways.
The first thing we could do is actually use them as the default property.
So here I'm gonna say first name, and then I'm gonna say the last name.
So that is equivalent to the exact same code, but I just saved five lines of code.
Right.
So, uh, before I was setting those properties in the constructor, I'm now
setting them as the default value and I can do other things on it, right?
If I wanted to trim it, for example, I could trim that property
and I'd be totally good to go.
Now, if I run it again, what we're gonna see is that I have the same
exact code, James font, Magno, same output, everything that we'd expect.
Now, let's say though that you're not actually really using.
These, uh, these properties, let's say that it was actually
just like a getter, right?
So here you could actually have that same exact code.
You're not allowing anyone to modify it outside of this class.
Well, You could just delete that, right?
Like what are you gonna do with it?
You're not gonna modify it, you're not gonna do anything.
You could just use those constructor parameters in the primary constructor
down inside of this last name, right?
So now I'm kind of minimizing my code cause I don't need those there, is
there because I wanna reference them or expose them, you know, like that.
Now a few things have changed here, right?
So, uh, if I run this, uh, structure and this and this code, again, we're
gonna get the same exact output Jameson.
The difference here is that when I do James Dot, I only
get full name and print name.
I no longer get first name, last name.
So it's kind up to you if you're gonna expose those properties or not.
But if you're not going to, you don't need anyone else to use them, then you
could just go ahead and delete them.
Now there's another thing that kind of bothers me in this
code, which is this logger.
So one thing we could do is also maybe shift that up actually
into the primary constructor.
So let's do that.
I'm just gonna add that onto the end.
And notice that kind of like the code is like, oh, that's cool, that's great.
But up here I can no longer, um, just create this person without the logger.
So let's go ahead and pass it in.
There we go.
Now I can go ahead and delete that from the code.
Now when we run this, we're gonna get the same exact output
as you would expect, right?
It's the same exact code.
So that's actually really, really neat.
We've actually now minimized this code.
We have our primary constructor, and you can do all sorts of things.
So if you wanna have more constructors, those constructors can call this,
and then they, of course, they could do anything they want.
So if you go and say public class, and you have a person.
And you're like, Hey, I really want, uh, to pass in just an eye logger, right?
And I could say every default person is, uh, oh, sorry.
Public person takes in an eye logger of, uh, l And then I could call this, I could
say, you know, James and then Montemagno, and then this could call the, uh, logger.
Just like this and I can do anything else inside of it.
Like I said, pass l there, there we go.
So you're still able to call the primary instructor, create more instructors.
Anything that you need kind of gives you that nice flexibility.
So there's my code.
I can see it to all on one page, just nice and minimized and like, this is a
nice class that is all available to me.
Now these things are very similar to, uh, record types, but they have different,
um, Uh, implications because these are not being exposed outside of the class.
You'd have to expose them yourself.
They are pr structor parameters that are then exposed as private
variables available to you.
All right?
So that's a cool example of what about in a real world application.
So here we have our Monkey Finder application, which
is a .NET Maui application.
And all I've done inside of here is done the same thing.
I enabled the Lang one version of preview here cause I wanna
be on the preview version.
Uh, so we're good to go.
Now when I look at the source code, We can see that this is my view model class.
It adherence from base view model.
Uh, it has a few PR properties here that are private, like a monkey
service, connectivity, geolocation.
Those are services that are being injected with the default, uh,
dependency injection service, which I've done a video on, on this channel
here, which is used in a p net core and Don Mau and other donnet projects.
So here we have a constructor, a monkey service i connectivity, IGeoLocation.
All this is doing is simply calling in and setting those things.
That's it, right?
We can also see that it's inheriting here from, uh, and calling down into the base
constructor, which is a monkey finder.
So if you look at the base view model, it'll take that in automatically
for us and set that property.
So what we could do is do the same thing.
We could shift this, right?
So let's go ahead and shift this up here, and then we can also shift over here
our call into our base of view model.
So now we're not only inheriting, but we're gonna call the base, uh, view model.
Over here as well.
So that's pretty cool.
Now what we're able to do is actually delete.
All of this code.
Look at this.
Eight through 16 gone.
Look how clean that class is, right?
And this is a public partial class.
We're getting auto-generated, um, uh, commands and
properties automatically still.
And now we have this one primary constructor taking in all of those.
And if we actually look at some of the other view models, like
the details of view models, we can see the same thing, right?
Look at this.
We have this, we have, um, we have our.
Our imap that we could pass in.
I'm just say map Now.
We're able to get rid of all this and of course we could, we could pass
in, uh, name for so details, right?
There we go.
So look at that.
We're just minimizing that code, streamlining that code,
making it super duper simple.
All right.
That is a whirlwind tour.
I wanted to show a simple example, but then also a real world example of how you
can actually clean up your code and make your classes with primary constructors.
Absolutely delightful to read.
Putting 'em all in one line.
I love it as we shift up, ship over and then make our.
Actual class is so much easier to read.
I love it.
Um, this is just one of the features in the C# 12.
I'll put a link to the blog post, um, below and also a great session, um, with
Mads and Dustin from.net com last year.
Uh, 2022 is absolutely great to take a look at.
If you have any questions or let me know how you feel about Primary
Constructors, leave your comments below.
I'd love to see it.
And if you like this video, give it a thumbs up jam, the subscribe button.
And of course, I put out videos here, um, just about every single week.
Anyways, hope you enjoyed it.
Look forward to your uses for primary instructors.
Thanks for watching.