Bald Bearded Builder
csharp/csharp-has-new-primary-constructors

C# Has New Primary Constructors

CodeSnap creates customizable, theme-aware screenshots of selected code directly inside VS Code.

Loads from YouTube when you press play

Transcript 3 topics

Primary constructors overview

0:00

C Sharp's new primary constructors can make your code cleaner and more concise, but there are some things you need to know. So let's talk about what they are, why you might wanna use them, and how to avoid any issues that might arise. Let's start with a constructor that's familiar. Our GeoPoint class takes in two parameters that are used to set private property. Of course, dependency injection is another great use case where we separate concerns of our app.

Regardless of the use case, the pattern remains the same and it's the same one we've been using in C sharp forever. But now primary constructors look to make this code cleaner. Let's take another look at that GeoPoint class, but refactored to use primary constructors in C sharp 12. You can see the constructor has been removed and the parameters for it have been added to the declaring types declaration line. Already that creates a reduction in code, but what really sets us apart as cool is that those parameters are now available within the scope of the entire declaring body.

Now this geopoint class represents like a geopoint on a map. So let's add some expression body members on it to use those parameters. And you'll notice it's so cool. You're able to access those parameters within those expression body members as if they were private members on the class or type.

Using constructor parameters

1:08

This also works for our dependency injected parameters and makes our code a lot cleaner and it keeps us from having to create private properties. One thing you'll figure out really quick is once you add a primary constructor, the implicit empty constructor is gone. But don't worry, you can still use overloads the way you did before, but you must call the primary constructor using the this keyword. If you're like me, this is pretty sick and you can't wait to go back and refactor some code or think about ways you wanna make changes.

But there are a few gotchas you should be aware of. The first is naming conflicts.

Primary constructor gotchas

1:42

The compiler will allow naming conflicts between primary constructor parameters and fields and property. And the way it handles this is basically the field or property override that parameter all throughout the body except for the case of initialization. So if you're declaring a property or a field and you're using that there, you'll you'll get the parameter from the primary constructor. Otherwise, you're gonna get that filter property. Primary constructors on record types have existed for a bit, but this behaves a little differently.

Unlike a record type, public properties aren't automatically created. So if you have a parameter with say latitude, it won't create a public property called latitude. You could do this yourself, but just be expecting that to be the case and you won't be able to use with by default. You'll have to add that property first. So the new class instruct primary constructors are pretty sweet and I'm looking forward to use them to make my code a little cleaner, concise and as long as we keep in mind those couple of gotchas, we'll be fine.

What do you think? Are you enjoying these new features from dot net eight? Let me know in the comments below and I'll catch you next time.

Sign in to join in. Reading needs nothing.