What are record types in C# and how they ACTUALLY work

Поділитися
Вставка
  • Опубліковано 18 гру 2024

КОМЕНТАРІ •

  • @ExpensivePizza
    @ExpensivePizza 3 роки тому +161

    I've been coding in C# for 20 years and I still learn a lot from your UA-cam channel. Thanks Nick!

    • @PaulSinnema
      @PaulSinnema 3 роки тому +6

      Me too. Thanks Nick.

    • @Alequez97
      @Alequez97 2 роки тому +5

      Are you that guy that bough pizza for 10k bitcoins?

    • @ExpensivePizza
      @ExpensivePizza 2 роки тому +4

      @@Alequez97 Laszlo Hanyecz paid 10,000 Bitcoins in 2010 for two large pizzas. Those pizzas now only cost 0.00135 Bitcoin. I'm not the guy, but I know the story ;)

    • @zenluiz
      @zenluiz 2 роки тому +1

      Same here

    • @prorakesh
      @prorakesh Рік тому

      15 here guy here.. when I see these features I realize what all I am missing.
      Great explanation in limited time. kudos.

  • @AlFasGD
    @AlFasGD 3 роки тому +32

    More specifically, records use the synthesis part of the lowering process. All these members are called synthesized, and it's common among the most widely adopted features of the language, including auto-properties. The compiler synthesizes those members for our convenience of not needing to write the same boilerplate code everywhere. Then, those synthesized parts are lowered into the more primitive constructs the compiler would like to care about when compiling into IL.
    Examples of other synthesized members include:
    - async/await methods (using iterators)
    - IEnumerable methods using yield (yield iterators)
    - records, as shown
    - auto-properties (using the backing field)

  • @damiank6566
    @damiank6566 Рік тому +6

    Been writing a C# for 5+ years now but I'm still impressed how Nick knows all these syntax tricks. For example, I never used "with" keyword, seems awesome though! Not mentioning the record types itself

    • @mattymattffs
      @mattymattffs 5 місяців тому

      With keyword is a trap

    • @holger_p
      @holger_p 2 місяці тому

      If they add new features to the language, it's normal, all your experience is completly unimportant.

  • @buriedstpatrick2294
    @buriedstpatrick2294 3 роки тому +23

    Records also serialize/deserialze very neatly. One issue I've had with using classes for this is the need for an empty constructor for HTTP request bodies. Essentially forcing the objects to be mutable. But with records you can have the best of both worlds. As in, enforce immutability with specific constructors while allowing for JSON serialization.

  • @mabakay
    @mabakay 3 роки тому +4

    14:07 "From record to this monster..." Like it! ;-)

  • @TheBdc884
    @TheBdc884 3 роки тому +7

    I am a senior backend developer that has 80% or more of personal experience on Java. I have had a freelance job NetCore doing some basic projects on it and decided to take a full time job on c# and increase my knowledge. Your videos have been both very educative and surprisingly interesting and amusing! I had to thank you Nick. Happy new year! Hop you will keep on delivering this great quality content!

    • @nickchapsas
      @nickchapsas  3 роки тому +2

      Happy new year! See you in 2022

  • @responsibleparty
    @responsibleparty 3 роки тому +13

    Thank you for this. I really didn't understand why record was added. Now, I see that is provided additional features but not in a more efficient way. So I would only use it if I really wanted the syntactic sugar it provides.

  • @MrJeeoSoft
    @MrJeeoSoft 3 роки тому +4

    I was convinced that record types were structures. Now it's more clear. Thanks for the video Nick!

    • @Krokoklemmee
      @Krokoklemmee 2 роки тому

      @@Qrzychu92 that's not how structs work in C#, nick actually also has a video about that

    • @diadetediotedio6918
      @diadetediotedio6918 2 роки тому

      @@Krokoklemmee
      It literaly is for the most

  • @odeyjoshuasunday998
    @odeyjoshuasunday998 Рік тому

    using with was amazing, you're a genius

  • @poniatowskimaximilian8618
    @poniatowskimaximilian8618 2 роки тому +1

    I knew that there is one man on the internet who can answer my questions, so I wrote "Nick Chapsas Records" in the search bar and I didn't get disappointed ! Thank you for so many contents on so many details/subjects !

  • @rmdg82
    @rmdg82 3 роки тому +24

    Interesting video. I'm wondering what are the ideal use cases for using record instead of simple classes. Dtos? Anything else?

    • @liveabhishekshukla
      @liveabhishekshukla 3 роки тому +4

      Yes after seeing this video... I am also eager to know the use cases...

    • @nickchapsas
      @nickchapsas  3 роки тому +23

      Dtos, shorthand queries or commands if you're doing CQRS with MediatR, objects that are immutable and you don't wanna bother with all the immutability implementations. The list goes on but really it is a class at the end of the day that implements what I described in the video so if you have the need for what the video showcases, then records make sense.

    • @tarekhaydar9795
      @tarekhaydar9795 3 роки тому +15

      The main reason is immutability, .Net reduces the amount of work to make a class immutable by inventing Records.

    • @thiagocustodio8177
      @thiagocustodio8177 3 роки тому +4

      I am so glad I am not the only one with this doubt

    • @ruekkart
      @ruekkart 3 роки тому +2

      In my team we recently started using them as commands instead of classes

  • @CeleChaudary
    @CeleChaudary 3 роки тому +2

    Hi Nick, I'm from Venezuela, and one of the things for that I watch your videos is because I can totally understand you even though english isn't my native language.

    • @holger_p
      @holger_p 2 місяці тому +1

      I think it's not Nicks first language either, and that can make a difference.

  • @NameSurname-zm7gl
    @NameSurname-zm7gl 3 роки тому +5

    Ok, from now number 69 is a Nick's trademark

  • @PaulSinnema
    @PaulSinnema 3 роки тому +2

    At 3:05 Nick shows code with a 'default!' value for the property. What does the '!' do?

    • @knutharald9814
      @knutharald9814 3 роки тому +3

      It prevents compiler warning about unitialized non-nullable property.

  • @rikk0011
    @rikk0011 3 роки тому +3

    I started using it just recently, thank for the video, this explains some of my questions!

  • @TheChlastak
    @TheChlastak 3 роки тому +2

    What does this default! exclamation mark mean? 3:03

    • @XeZrunner
      @XeZrunner 3 роки тому

      I would like to know that as well.

    • @lifelover69
      @lifelover69 3 роки тому +2

      The exclamation mark postfix is the null-forgiving operator, introduced in C# 8.

    • @nickchapsas
      @nickchapsas  3 роки тому +3

      It means, “if I dont provide the value, set this property to the not null default value for this type”

    • @vivan000
      @vivan000 3 роки тому +2

      @@nickchapsas you're very wrong and `default!` should be used with huge caution.
      Default for string is null, so `default!` equals to `null!`. It initializes that string property with null, but, at the same time, asks to forgive it.
      So the only thing it's doing is removing warning (CS8618: Non-nullable property must contain a non-null value when exiting constructor. Consider declaring the property as nullable).
      You will have null in that field if you don't set it, but you've asked compiler to ignore this possibility it...

  • @salarkazazi7584
    @salarkazazi7584 3 роки тому

    One of most worthwhile channels to visit. Thank you so much.

  • @mohamedelhakim5966
    @mohamedelhakim5966 3 роки тому +1

    Really enjoyed that video

  • @barwalgayatri4655
    @barwalgayatri4655 Рік тому

    One of the Best Info

  • @itsquick7250
    @itsquick7250 2 роки тому +1

    Whats the ! (default!) in 3:02?

  • @infinityminuszero
    @infinityminuszero 2 роки тому +1

    Dammit man! You taught so much in 15 minutes. Especially learning of sharplab was amazing for me. Thanks for your work!

  • @syriuszb8611
    @syriuszb8611 3 роки тому +3

    What does the syntax of .$() do? Is this something specific for lowered code? Why not just .Clone()?

    • @babafemiogundipe1453
      @babafemiogundipe1453 2 роки тому

      I think there is a clone method below that create a new instance of the class

    • @syriuszb8611
      @syriuszb8611 2 роки тому +2

      @@babafemiogundipe1453 I was refering to syntax, not method, what does $ mean?

    • @aliengarden
      @aliengarden 2 роки тому +1

      @@syriuszb8611 I am wondering as well. Did you figure it out?

    • @Hantick
      @Hantick 2 роки тому

      Wondering as well

    • @syriuszb8611
      @syriuszb8611 2 роки тому

      @@aliengarden Sadly, no. My only guess is that it's specific for lowered code, I don't think that we can use it normally.

  • @lordicemaniac
    @lordicemaniac 3 роки тому +2

    can you use that $() method on any object? or is it usable only in lowered code? can anything used in lowered code be used in normal code?

    • @nickchapsas
      @nickchapsas  3 роки тому +2

      Yes it is part of the ICloneable interface

  • @artursabirov8021
    @artursabirov8021 2 роки тому

    Hi, Nick! I wanna thank you for your videos, when I watch it then I learn English and .Net in the same time

  • @aliengarden
    @aliengarden 2 роки тому

    I haven't had this much fun looking at code in a while. Sublime video!

  • @aughey
    @aughey 3 роки тому +5

    I recently resurrected an old project that was giving me grief. I upgraded the projects to the latest compiler and runtime sets, converted a lot of data code into records, and while not a silver bullet, the compiler generated capabilities of records relieved a lot of issues and code bloat.

  • @Andrew90046zero
    @Andrew90046zero 2 роки тому

    What the heck is that syntax for at 15:13 where it looks like "k__BackingField" and also "$()" ?????
    I'm just curious.
    Also, I was kinda hoping you were also going to say that Records have the ability to be more effectively cleaned up by the GC. Because it seems like they are supposed to act as value types but with references. So if you create an instance of one in a function. It may be able to automatically free up the data when the function returns. So fewer allocations?

    • @nickchapsas
      @nickchapsas  2 роки тому

      Records are classes (unless it’s a record struct). They don’t have fewer allocation.

    • @Andrew90046zero
      @Andrew90046zero 2 роки тому

      @@nickchapsas ​ Alright cool, but what was up with that special syntax!
      k__BackingField

    • @nickchapsas
      @nickchapsas  2 роки тому +2

      @@Andrew90046zero It's just how the compiler behind the scenes will generate the backing field and name it in a way that it can't clash with something that you have in your code

  • @BEN-ys6gu
    @BEN-ys6gu 2 роки тому +2

    Ok this is the kinda stuff that makes me want to learn C#. The newer versions have some really really good features.
    (I am already familiar with the language, but I want to learn more)

  • @abdelrhmanahmed1378
    @abdelrhmanahmed1378 2 роки тому

    nice video

  • @adabsurdum7011
    @adabsurdum7011 2 роки тому +1

    Thank you for your content. Helps me a lot with further training.

  • @EduardoAG
    @EduardoAG 3 роки тому +1

    Can you use Records to serialize and deserialize json?? I mean If I change all my classes to records in my ASPNet core project it will still working?

    • @robertmrobo8954
      @robertmrobo8954 3 роки тому +1

      Yes that will work. On one my projects, On the Query stack, I'm using records as my DTOs to read data from Cosmos DB. Which are serialized from Json.

    • @EduardoAG
      @EduardoAG 3 роки тому +1

      @@robertmrobo8954 Nice. Thanks!

  • @bnotorious1
    @bnotorious1 3 роки тому +1

    Nice video and explanation Nick! I noticed that this type of special class record acts like a valueObject. Can you give us an example on how to use it in a real world scenario? I will be grateful.

    • @MidnightSt
      @MidnightSt 2 роки тому +1

      real world scenario?
      to represent rows in your DB ORM framework.
      or more generally, for DTOs, where they will be more comfortable to work with than normal classes.

  • @HamzaKhan-zd6qy
    @HamzaKhan-zd6qy Рік тому

    Very good and helpful.......

  • @stephenwood4814
    @stephenwood4814 2 роки тому

    An excellent and lucid explanation.

  • @marianvdovin2455
    @marianvdovin2455 6 місяців тому +1

    so are there any reasons not to use it all the time? As I see it is a class but with extra things for free. If you don't build high performance application then I think it is the future or what?

    • @holger_p
      @holger_p 2 місяці тому

      It's not inheritable. If you don't want to use virtual methods and anything related, I would agree. But you need polymorphism to design applications.

  • @yp5387
    @yp5387 Рік тому

    I am still confused thought. I get what is Record type in how it is different from Class. But what is the real use case? Where do I use Records in IRL projects, and why should I use Records instead of class? In the person example, would EF work with record type? if no then I will have to create both Person class and Person record. which is duplicaiton.

    • @holger_p
      @holger_p 2 місяці тому

      It's less typing. If you run into having 2-3 class types for a person anyway, then you will discover the usefulness of records.
      If EF is creating types for you, you don't have to bother if it's records or not. They will behave like records definitly.

  • @mattymattffs
    @mattymattffs 5 місяців тому

    It's syntactic sugar for dictionaries

  • @barwalgayatri4655
    @barwalgayatri4655 Рік тому

    Please make a Video on If Interface 8.0 can Implement methods , then do we no longer need Abstraction now ??? I know the Basic difference that Abstract cant support Multiple Inheritance but Interface ca... but then whatsss the use of Abstract then ????

  • @brynarn
    @brynarn 3 роки тому

    Hello Mr Nick

  • @CliveGregory
    @CliveGregory 3 роки тому

    Any reason why the record type's ToString() method doesn't include quotation marks like JSON?

    • @nickchapsas
      @nickchapsas  3 роки тому +1

      Because it isn’t json. It is just a formatted string

  • @aah134-K
    @aah134-K 3 роки тому

    How to serialize? Or save in a table in db without any id? Would you add id fir the db?

    • @aah134-K
      @aah134-K 3 роки тому

      I dont know what to ask however I am trying to say would you use it as a value object? Which in definition have no id field

  • @metaltyphoon
    @metaltyphoon 3 роки тому

    Didn’t the CLR have to change to support the idea of init ? Or was it already there because of F# ?

    • @nickchapsas
      @nickchapsas  3 роки тому

      It didn't because it isn't a CLR thing, it is a complier limitation thing.

  • @barmetler
    @barmetler 3 роки тому +1

    How is it able to call the setter of FullName even though there isn't one? Maybe because "init" is actually a setter, and lowered code has the privilege of calling it even after initialization?
    I mean I know that lowered code doesn't have the same restrictions as normal code, but this kinda confuses me...

    • @nickchapsas
      @nickchapsas  3 роки тому

      Init is basically an initialisation only setter. Properties are backed by readonly fields so even if there isn't a setter, you can still initialize a property in the constructor as if it had one, same as you would with a readonly field.

    • @barmetler
      @barmetler 3 роки тому +1

      @@nickchapsas I know, but this isn't happening in the constructor. In the lowered code, it first clones the object, and _then_ overwrites all the to be modified fields.

  • @dyrL90
    @dyrL90 2 роки тому

    Great video and explanation! Thank you very much.

  • @MrAndrei4777
    @MrAndrei4777 3 роки тому

    Very detailed as always, thanks!

  • @xavier.xiques
    @xavier.xiques 3 роки тому +1

    Very interesting. Thanks a lot!

  • @clashclan4739
    @clashclan4739 2 роки тому

    what is that base..ctor() in lowered code

  • @FernandoTakeshiSato
    @FernandoTakeshiSato Рік тому

    dude, awesome video, thanks!

  • @Delphi80SVK
    @Delphi80SVK 3 роки тому

    Nick, can you explaing why lowering code contains "DateTime" type for "DateOfBirth" when original code contains "DateOnly" type? Is there any other underthehood magic which converts DateOnly to DateTime and vice versa?

    • @nickchapsas
      @nickchapsas  3 роки тому +2

      I actually don’t know that

    • @Delphi80SVK
      @Delphi80SVK 3 роки тому +1

      @@nickchapsas maybe there are operators overwritten from/to dateonly and datetime. Or bug in that website.

    • @DFPercush
      @DFPercush 3 роки тому

      If I may provide a total guess, most date/time types may just be a 64 bit long containing a unix-style timestamp that gets calculated for the local date and time as needed. The DateOnly restriction probably just imposes a certain granularity on the values, like it can only be mod 24 hours or something.

  • @SzpkRomana
    @SzpkRomana 2 роки тому

    dude this is so good

  • @KeithBarrowsToday
    @KeithBarrowsToday 3 роки тому

    Way outside of the topic at hand - trying to duplicate this code line for line in VSCode - not compiling at all. Missing so many curly braces. Is this a feature of your editor?

  • @pagorbunov
    @pagorbunov 3 роки тому

    What if the record type object has a property with a reference type? How the equality check will work? Just a references equality check?

    • @Grimlock1979
      @Grimlock1979 3 роки тому +2

      It uses EqualityComparer. Default.Compare for each property. If T has not implemented any kind of equality, it will result in a reference comparison.

    • @pagorbunov
      @pagorbunov 3 роки тому

      @@Grimlock1979 ty

  • @HamishArb
    @HamishArb 3 роки тому +1

    Good video! I was also hoping that you'd touch on the init keyword and modreq/modopts. Do you have one on those?

    • @nickchapsas
      @nickchapsas  3 роки тому

      I am not aware of modreq/modops. I guess it’s reading time 😀

    • @HamishArb
      @HamishArb 3 роки тому

      ​@@nickchapsas I look forward to the video if you make one :) they are used extensively in C++/CLI I believe (eg. const keyword on a parameter causes it)

    • @alexandervasylevskyi1332
      @alexandervasylevskyi1332 Рік тому

      @@nickchapsas that's the answer why you know that much and willing to share. Because you learn as well, right? :) Great job Nick, I am really is amazed by quality of your videos and starting to think of making a channel myself. Reading the comments under your videos I sometimes have a lot to share but not really fan of doing it in UA-cam comments :)

  • @luan_maik
    @luan_maik 3 роки тому

    Is this a good aproach to use in Value Object?

    • @nickchapsas
      @nickchapsas  3 роки тому +3

      Kinda. Value objects also have some extra stuff that you need to implement to have a "true" implementation but it is a good starting point

  • @sahawndada
    @sahawndada 2 роки тому +1

    How to watch a Nick Chapsas video.
    1. Smash the like
    2.Enjoy the content 👌

  • @arpanmukherjee4625
    @arpanmukherjee4625 Рік тому

    Basically a Typed Tuple kind of thing.

  • @arjix8738
    @arjix8738 2 роки тому

    So a record is like a data class?
    Or did I get something wrong?

  • @artemyur7162
    @artemyur7162 2 роки тому

    Nice content!

  • @carstenberggreen7509
    @carstenberggreen7509 Рік тому

    I noticed your last part (14:30) has a datetime set to 1993, 4, 20 ? is that from a different example before the one in the beginning?

  • @viktordoe1636
    @viktordoe1636 3 роки тому +1

    "I will not buy this record; it is scratched."

  • @vbaclasses3553
    @vbaclasses3553 3 роки тому

    So record is the same as dataclasses in python?

    • @nickchapsas
      @nickchapsas  3 роки тому +1

      Yeah, or records in Java or data classes in Kotlin or bla bla. It’s a very common feature in many languages

    • @vbaclasses3553
      @vbaclasses3553 3 роки тому

      Nice that C# is also including it.

  • @clashclan4739
    @clashclan4739 3 роки тому

    why Microsoft didn't mentioned about lower nowhere in its docs?

    • @nickchapsas
      @nickchapsas  3 роки тому +1

      I would assume because it is an implementation detail that you don’t “need” to know about

  • @PapaCarlo87
    @PapaCarlo87 2 роки тому

    Great as always,thank you very much Nick!

  • @mzeeshan
    @mzeeshan 2 роки тому +1

    Cool video mate!. Does that mean we can add our own custom features like Record to the C# without touching the CLR and using the lowering mechanism?

  • @johnbleichert8322
    @johnbleichert8322 2 роки тому

    Great video - thanks! Exactly what editor or development environment is that?

  • @XKS99
    @XKS99 2 роки тому

    I heard they are going to rename C# 11 to F#

  • @shanksatutube
    @shanksatutube 2 роки тому

    Hey Nick, what are the contents on the C# testing course?

    • @nickchapsas
      @nickchapsas  2 роки тому

      You can see the full curriculum in the course’s page

    • @shanksatutube
      @shanksatutube 2 роки тому

      @@nickchapsas thanks. but looks like the YTDEP2 coupon expired. bummer :(

  • @jhonsondoe407
    @jhonsondoe407 3 роки тому

    Hey Nick. Great video, Do you think we can use record for database entities instead of classes?

  • @programuoki-lt1465
    @programuoki-lt1465 3 роки тому

    kotlin data class same as record :)

  • @lollo4711
    @lollo4711 2 роки тому

    How do I initialize a record straight? Like:
    private record myData(string Name, int Age) = new("ABC",1);
    Couldn´t find any matching syntax to do it.

  • @CookieMonsteeerrr
    @CookieMonsteeerrr 2 роки тому

    On the line where u have "var nickbutOlder = nick with.." - as u shown when u compare nickbutOlder with nick it will say that they both not same reference, but i tried and removed 'with keyword doing "var nickbutOlder = nick" and in this case reference is the same (same as with classes) - why then with "with" it will be not same reference and without it will?

    • @nickchapsas
      @nickchapsas  2 роки тому

      Because with will clone the object which creates a whole new object with a new reference

  • @cruz1ale
    @cruz1ale Рік тому

    10:37 mmm... sweet cheese

  • @toss2151988
    @toss2151988 3 роки тому

    Hello thx for video! :) Please is Dependency Injection course also for Unity/C# developers? :)

    • @nickchapsas
      @nickchapsas  3 роки тому +1

      There is a lot of focus on ASP.NET on the course but there are a lot of non-ASP.NET stuff there too.

  • @logan577
    @logan577 3 роки тому

    gud vid

  • @parzivallopez1272
    @parzivallopez1272 3 роки тому

    came in 50 seconds. Now make a course on c# and .net core

  • @Gray-Wolf-Media
    @Gray-Wolf-Media 3 роки тому

    I was working on a WASM project type yesterday, setup the models, the DbContext and setup the DI in the Program.cs file. Then I went looking for the appsettings.json file and there is none. It is looking for deps.json file that doesn't exist either. So I created one to what it is looking for and afterwards after setting up the ConnectionString and injecting it into the SqlConnection it returns an error of Key Not Found. Try it and let me know what you think...

  • @chrisfelix9065
    @chrisfelix9065 3 роки тому

    This is same as data class in kotlin

    • @nickchapsas
      @nickchapsas  3 роки тому

      Or data class in Python, or record in Java or

  • @AhmedAbdulla2020
    @AhmedAbdulla2020 3 роки тому +1

    You know that the next video will be a perf comparison 😅

  • @pqsk
    @pqsk 3 роки тому +2

    Keep coding...you say it in such a way that you're brainwashing us. I love it. 😆

  • @robl39
    @robl39 2 роки тому +1

    If only the C# language designers created a “ref struct record class value task span” feature…

    • @AveN7ers
      @AveN7ers 2 роки тому

      The language is definitely bloated

  • @yasinfallah7327
    @yasinfallah7327 3 роки тому

    دوست من امکانش هست که آرام تر صحبت کنی و بدون لهجه تا تمام ملیت ها متوجه بشن؟

    • @micalobia1515
      @micalobia1515 3 роки тому

      His accent isn't actually very strong, for me. Personally, I think his accent actually makes him easier to understand. He could go slower though, I understand that completely, I think he goes fast so that the video isn't longer than it needs to be. It might be a good idea to change the playback speed to a lower value if that's something you can handle.

    • @nickchapsas
      @nickchapsas  3 роки тому +8

      I prefer having some people slow me down than having a lot of people speed me up. I make content that I would like to watch myself and my biggest pet-peeve is slow content. I totally get how it is not for everyone but it is what most people find attractive to the channel, that I respect their intelligence and time

    • @TeddyBearItsMe
      @TeddyBearItsMe 3 роки тому +3

      Omg please no
      One of the things I like about this channel is the speed.
      15 minutes to explain what would normally take hours in a classroom.
      And he is perfectly understandable, no idea what kind of accent you want him to talk with :/

    • @Hantick
      @Hantick 2 роки тому

      @@TeddyBearItsMe Nick's accent is fine. I understand almost every word.

    • @TimmyBraun
      @TimmyBraun 2 роки тому

      @@nickchapsas I love your fast content :), I usually still speed it up to 1.5x (occasionally 2x). Most other similar content is 2x for me

  • @flogginga_dead_horse4022
    @flogginga_dead_horse4022 Рік тому

    Stolen from F# like many things lol