Primary Constructors in .NET 8

preview_player
Показать описание
One of the new changes to the C# language is the primary constructor. Now, there has been some confusion around this topic, about why it exists, what purpose it serves, and how you would use it for various scenarios. In this video, we will look at what primary constructors do and how we can use them safely and effectively.

Рекомендации по теме
Комментарии
Автор

Thanks for answering my question Tim. I noticed that you are going through all of the comments with questions and addressing them. Thanks for your hard work and dedication. Keep up the good work producing content that is approachable, even pleasant, to view. It often feels like I'm on a call with an old friend reminding me of something that I forgot. :)

DarrylTaylor-yo
Автор

New to the channel and also a new .net developer. The comment section is valuable to me as I read the interactions between what the other senior developers concerns are and your comments. Thank you for being candid on your responses.

jeffsherman
Автор

That was very instructive. I've noticed it a couple of days ago when I finally decide to upgrade a blazor app from 7 to 8. This video comes just in time, thank you Tim.

AndreiHetel
Автор

Great explanation Tim, I liked the conclusion you provided "if it make code more readable the go for Primary Constructors if not then don't..." I beleive that pretty much explain how to use this feature.

danielvillalba
Автор

I gotta be honest. With both C# and .Net, I kind of feel like a lot of effort is going into solving problems that I never had.

I still have to learn all the new ways, so that I can recognize and understand them when I see them. But given that copilot will type my boilerplate for me anyway, I don't see much advantage in saving 3 lines of code.

Also, OCD me might actually have to go back and chance all my DI to the new way, just so my code doesn't mock me in my sleep.

benjamininkorea
Автор

I was waiting for this one from you. Sometimes I'm too busy to look out new features.Thanks

fqlt
Автор

As a JavaScript main, i welcome this C# change!

cirusMEDIA
Автор

I think I see your point on why this is cleaner and how you can enforce the readonly in the DI example. I honestly don’t think either way of constructors is better than the other, really just comes down to preference at the end of the day.

parkerwarner
Автор

Sir, your explanation has greatly clarified things. Thank you.

andergarcia
Автор

Very very nice! Thanks so much for such a great explanation!

animaniaco
Автор

Nice and detailed explanation with recommendations. thx

gopikrishnag
Автор

Love this syntax especially in di situations, minimizes repeated code.

purplepixeleater
Автор

I love this Syntex but currently I have plan to not use it until next version of c# that would probably introduce read-only support

aathifmahir
Автор

As a newbie, I think a primary constructor is a more naturally-understood way to start a class.

The .NET7 style constructor was something that took me ages to grasp, as
1) I hadn't understood it's purpose- this is because I hadn't yet separated the concepts of instantiation and declaration of an object.
2) The mental overhead of things to remember for "just starting a class". The .NET7 ctor is verbose - it appears that the same variable and class name x2, and having the subtlety of parameter vs property, but also with specific arbitrary syntax conventions for these objects (i.e. caps, no caps, leading underscore).


If you want to get to the "do something" part of the code, I think the primary constructor will mosty apply to get you there, with the config options on the .NET7 style ctor being something to learn in the next lesson :)

DavidKershaw-xl
Автор

What is the behavior if you were to mark the class as partial? I'll need to try this to see.

mattc
Автор

When using primary constructors with DI, in this example, how can we log the time the object is created? Seems we can't. No constructor. I thought you would mention something to the effect, or have I misunderstood the concept or missed a trick to do this?
If we do, some error-prone boilerplate code starts creeping in again. I have to say, I don't think I'm a fan of this.
I often update UI-bound labels with information passed by a service when the ViewModel is constructed, and all benefits are lost.
PS: You are my go-to channel for C#, Thanks!

jonnewcomb
Автор

For me the DI assign field thing doesn't solve it yet because it generates it without the underscore. It may seem silly but when doing that kind of boilerplate stuff i look for speed, and if the suggestion is wrong and I have to manually put the underscore then it doesn't work for me. Great vid still, trying to showcase most of the use cases.

zagoskintoto
Автор

I definitely see a usage for it, especially in combination with DI. Makes it a lot cleaner and more readable.👍

falksweden
Автор

Tim, on the bottom left side of the editor, to the right of the Copilot icon, there is a purple icon that allows you to turn off suggestions.

BachiMjavanadze
Автор

Seems a bit of a pointless feature that will probably lead to more readability issues when some one else comes to fix a bug or add a feature in the future. When most devs probably create and instantiate class levels fields from the constructor parameters with the existing short cuts then what is this saving?

DACA