Primary Constructors in C# 12 Explained!

Primary Constructors in C# 12 Explained!

James Montemagno

1 год назад

24,029 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@MohammadNikravan
@MohammadNikravan - 19.11.2023 12:44

Assigning the primary constructor to a fullset property results in having two fields in your class - one for the field and one for the primary constructor. Unfortunately, we can't make the generated field as read-only in the primary constructor. If we assign parameter to a property it might cause the object to consume double the memory or at least bug prone as we have to make sure which one is used in the class. If you bypass the primary constructor to the parent, the field will exist in derived class too! This feature can be quite confusing, and I wish there was an option to disable it completely to prevent our team from using it.

Ответить
@Forshen
@Forshen - 29.10.2023 12:33

I find this kind of code disgusting. There is too much happening under the hood and you have to have more knowledge about the feature / flow of the code. Your default constructor gives us a scope and a 'main line' about the executing of the code and how it all connects together.

Ответить
@gregdennis6094
@gregdennis6094 - 30.05.2023 23:57

I know it's demo code, but.... WHY DOES YOUR PERSON HAVE A PRINTNAME() METHOD?! And then.... WHY ARE YOU INJECTING A LOGGER INTO IT?! These dependencies are so wrong!

Ответить
@WhiteCapix
@WhiteCapix - 24.05.2023 19:34

To be honest looks neat, but when you replaced the MonkeysViewModel with the primary constructor we don't have any longer the whole code on the file in front of us, we need to scroll sideways, which for me is not super. We could get some line breaks and still be fine, I would say.

Ответить
@NemanjaSimovic
@NemanjaSimovic - 24.05.2023 18:20

Looks like you missed to say that primary constructor creates private field with the same name as its parameter. Nice video thou. Thanks.

Ответить
@RusWatcher
@RusWatcher - 23.05.2023 11:09

it only makes the code harder to understand. everything is scattered around the class.

Ответить
@Lactorioga
@Lactorioga - 22.05.2023 18:40

O M G what a shit... almost like using without {} brackets but even worse in terms of readability. Hope this "feature" die by no usage.

Ответить
@eriknyk2k
@eriknyk2k - 21.05.2023 02:45

I can see that it's borrowed from kotlin and I love it

Ответить
@Eirenarch
@Eirenarch - 21.05.2023 02:35

The savings are not worth it the insane scoping rules. i'd rather have them port the records primary constructors to classes so at least they can be used to declare properties

Ответить
@gds03_
@gds03_ - 20.05.2023 23:04

what about the underscore convention for variables, specially private ones? defining a area of variables with _ to me helps a lot!

Ответить
@chuahanjon92
@chuahanjon92 - 20.05.2023 20:28

does it work with mapster's MapToConstructor?

Ответить
@diligencehumility6971
@diligencehumility6971 - 20.05.2023 18:00

C# were perfect around C# 8 or 7. Now it's completely bloated. This is exactly how it became bloated. "We want to make programmers write less lines" so now you have weird symbols and strange syntax everywhere that's nobody understands, it's less readable, it's not helping anything really, yeah it took 1 sec less to write my class hurray, now it's difficult to understand what's going on. Please stop it.

Ответить
@krccmsitp2884
@krccmsitp2884 - 20.05.2023 16:30

I'm indecisive whether I like it and will use it, or not. What bothers me mostly is the different behavior to records for the same syntax.

Ответить
@ak_nex
@ak_nex - 20.05.2023 12:25

This primary constructor sucks...

1. Most people prefer private field names with underscores. How stupid it would be to have smth like this: Person(string _name)...

2. And what about records? Their primary constructors behave differently. The parameters are not private fields but properties with "get" and "init" modifiers.
It would make more sense to have the same implementation in both cases then

Ответить
@kolesplace
@kolesplace - 20.05.2023 04:54

I love these new features. If I'm hopeful my employer will adopt C# 12 by 2030 😂😂

Ответить
@stephenmiller4948
@stephenmiller4948 - 20.05.2023 04:51

This is pretty cool, James! But… How does this work with DI? The DI container had to have been getting the class instantiation signature from the constructor. Does this require a newer version of the container?

Ответить
@ryanye6938
@ryanye6938 - 20.05.2023 03:35

dude you gonna loooooove typescript

Ответить
@ahmad.mozaffar
@ahmad.mozaffar - 20.05.2023 00:24

Thanks

Ответить
@dmitrymezinov9238
@dmitrymezinov9238 - 19.05.2023 23:31

hmm... interesting feature, but where to check constructor params (for null, as example) and throw exceptions if necessary? isn't that common practice to do such in constructors?

Ответить
@philippelhaus
@philippelhaus - 19.05.2023 22:54

Useful

Ответить
@MartinMaat
@MartinMaat - 19.05.2023 22:49

So first they introduced multiple property styles which was undesirable and stupid (as if anyone would want their language to facilitate mixing different styles that do the exact same thing, we don't mix formatting styles, we didn't need this). They must have figured you still had too much opportunity to focus on your coding logic. You would rather use a part of your brain to figure out different constructor styles as well, would you not? Let's clutter the class declaration some more, just the class name, a base class and a number of interfaces is way too easy, let's throw in some arguments for no ffin' benefit at all. Way to go C# designers.

Ответить
@mtranchi
@mtranchi - 19.05.2023 21:55

like it!

Ответить
@samuelmbah8789
@samuelmbah8789 - 19.05.2023 20:54

Nice video James. For someone who's written C# for less than a year, this feature looks cool to me 😄. I'll try it out

Ответить
@mwetzel0
@mwetzel0 - 19.05.2023 20:24

I'd take robustness and readability over saving lines of code. But, if this makes (some) code more robust and readable, than I'm all for it. I'm not sure I'll personally use the feature because I see too many cases where it's detrimental (like giving up parameter checking) and I'd rather my code be consistent (versus some using primary constructors and some not).

I don't think allowing to create a Person with, say, a null name or a whitespace-only name, qualifies as robust. I'd prefer to throw an exception at construction rather than have some hard to track down condition where a bogus property caused a bug elsewhere in time and code.

Ответить
@ignispurgatorius5297
@ignispurgatorius5297 - 19.05.2023 20:11

I don't see myself having much use for the example around the logger, because usually that would just mean I have to pass more stuff that I might not need in most cases, so the use case feels a bit forced there. But I can definitely see the use case around having a primary constructor for myself when declaring small classes that are mostly used to shovel data between interfaces. Those will definitely look neater that way.

Ответить
@dasfahrer8187
@dasfahrer8187 - 19.05.2023 19:59

Neat. How nicely does it play when the [JsonConstructor] decorator is needed on one of the constructors? Also, how will this play with the Windows Community Toolkit?

Ответить
@scotty3024
@scotty3024 - 19.05.2023 19:28

C# is becoming more like Kotlin every day

Ответить
@waleedbensumaidea3947
@waleedbensumaidea3947 - 19.05.2023 19:11

Thank you so much James🌹. This is very helpful

Ответить
@fayazvar3948
@fayazvar3948 - 19.05.2023 19:06

If i have some logic in Constructor.

Ответить
@HOSTRASOKYRA
@HOSTRASOKYRA - 19.05.2023 18:54

When will they finally add a constructor method, without a class name?

Ответить
@Sindrijo
@Sindrijo - 19.05.2023 18:49

Does this mean we also get expanded generic constraints for constructor signatures?

Before you could only constrain a generic to have a default constructor with no params e.g. SomeClass<T> where T : new()

Ответить
@grumpydeveloper69
@grumpydeveloper69 - 19.05.2023 18:14

I see a benefit for structs and records where the parameters are the data being stored. But, what about constructors that have move than just assignments, and have calls to say a private initialisation method. That is not compatible with primary constructors right?

Ответить
@Kokujou5
@Kokujou5 - 19.05.2023 17:29

finally... no longer triplicated code when dependency injection... it's a dream come true. it's so frustrating, first you need them in the constructor, then inside the constructors bvody and secondy you need them as private readonly properties inside your case. triplicated code.

i was so close to switching all my classes to records just because it was so annoying but now it's finally coming to classes! now i'm happy <3

Ответить
@truebzyq
@truebzyq - 19.05.2023 16:55

Coding is not about "saving lines of code" but about readability, so all sexy blink blink code shortcuts are simply making language less readable, and less bugproff, and imagine that right now you are start learning programming in 2023 and someone just give you that class, with primaru constructor but you need to learn at first what constructor is, and the question is why? Why you have used 'l' in seconda constructor? Any problems with the same property name? Offcourse and then try to described it to a person that that just start learning programming. Its simply a nightmare.

Ответить
@aathifmahir
@aathifmahir - 19.05.2023 16:49

It would be great, if the c# team adds an option to make fields readonly by using the ReadOnly Keyword in the Primary Ctor

Ответить
@novatroop77ni
@novatroop77ni - 19.05.2023 16:35

Its def a game changer but did you no about the = null trick on your interfaces I didnt until a more senior then myself pointed it out.

Ответить