Coding Shorts: For The Record - Why You Should Use (Records in C#)

แชร์
ฝัง
  • เผยแพร่เมื่อ 5 ก.พ. 2025

ความคิดเห็น • 70

  • @raphaellukas3328
    @raphaellukas3328 ปีที่แล้ว +6

    When I was looking for a video to explain the C# record, I knew I was in good hands. Always to the point, no big talk around it. Many greetings from Germany

  • @jemakrol
    @jemakrol ปีที่แล้ว +4

    First time visitor. Senior dev that just wanted to see some different explanations on the topic in the context on how to best describe it.
    You explain in a concise and pedagogical way. You do this without all the the Bells and Whistles that usually comes with TH-cam videos. I hesitate to search for explanations in video format because it often comes with too much distraction and ambition. Motivated or not, things I didn't want. This, however, really gets to the point in a straight forward and relaxing manner. Nice!

  • @rmcgraw7943
    @rmcgraw7943 2 หลายเดือนก่อน +1

    Value type equality semantics and immutability, mostly. Good with event sourcing.

  • @godfathermikal
    @godfathermikal ปีที่แล้ว +3

    Sweet. That makes sense. Didn't really see how record was useful, but this makes sense. Thanks!

  • @JohnSmith-op7ls
    @JohnSmith-op7ls ปีที่แล้ว +1

    You should mention how and when a record will be allocated to the heap vs stack as this is important for performance and GC. Also, go into when and how copying of a record instance is a fill/deep copy, meaning no references are reused, and when it is a shallow copy, meaning value types are copied but reference types only have the pointer copied.

    • @stxnw
      @stxnw 11 หลายเดือนก่อน

      this is standard knowledge so there’s no need to re-explain this.

  • @timothyvandyke9511
    @timothyvandyke9511 ปีที่แล้ว +6

    I would absolutely love if this encourages people to put a bunch of (related) types into the same file so we don't get a bajilllion files just for simple types.

  • @chairmakerPete
    @chairmakerPete 6 หลายเดือนก่อน

    That was like a super-concentrated C# learning syrup. Brilliant!
    Not sure I'll ever use records, but it's great to at least understand what they're for as I'm sure a use-case will pop up at some point.
    Great work - thank you!

  • @jasondelauro
    @jasondelauro 11 หลายเดือนก่อน

    You have an excellent concise teaching style.

  • @kodindoyannick5328
    @kodindoyannick5328 3 หลายเดือนก่อน

    Short and simple! Thanks!

    • @swildermuth
      @swildermuth  3 หลายเดือนก่อน

      You bet!

  • @tytz100
    @tytz100 ปีที่แล้ว

    this was the clearest explanation video I've ever seen in a while. GJ!

  • @DmitriyBelous
    @DmitriyBelous ปีที่แล้ว +1

    Great explanation, thanks!

  • @AshrafSada
    @AshrafSada 5 หลายเดือนก่อน

    Thank you, you are great

  • @TheAndronKolaider
    @TheAndronKolaider 9 หลายเดือนก่อน

    The best explanation you can ask for

    • @swildermuth
      @swildermuth  9 หลายเดือนก่อน

      Thanks. I am glad it helped.

  • @Samwidges21
    @Samwidges21 10 หลายเดือนก่อน

    Brilliant video, that was so concise and informative, thank you.

    • @swildermuth
      @swildermuth  10 หลายเดือนก่อน

      Glad it was clear!

  • @NoonKnite
    @NoonKnite ปีที่แล้ว

    great explanation! i've struggled with how classes and recs are different, so this really helped a lot.

  • @AlexapolloRodopoulos
    @AlexapolloRodopoulos 10 หลายเดือนก่อน

    Amazing explanation and demonstration keep it up!

    • @swildermuth
      @swildermuth  10 หลายเดือนก่อน

      Thanks, will do!

  • @rogerstich7603
    @rogerstich7603 ปีที่แล้ว

    First of all, even if i point out a flaw, i really love your shorts, this video helped me alot in finding a simple way to explain this construct to my trainees and junior devs.
    The flaw im talking about starts at 1:50 of your video.
    There you are telling that you make a copy of the product by writing this:
    var product2 = product;
    But thats not true, you dont make a copy you just assign the pointer to another variable, you copied the pointer, not the object.
    You can test it by using this Code:
    var product2 = product;
    product.Description = "Description of Product";
    Console.WriteLine($"Product1: {product.Description}");
    Console.WriteLine($"Product2: {product2.Description}");
    If i had a Copy, then only the first Console WriteLine should contain the Description string "Description of Product".
    But both does and thats because those variables actually do point to the same object and for that makes your explaination later with equals a bit weird.

    • @swildermuth
      @swildermuth  ปีที่แล้ว +1

      I didn't mean to imply that. My bad.

    • @rogerstich7603
      @rogerstich7603 ปีที่แล้ว

      @@swildermuththanks for the response

  • @DevSharp01
    @DevSharp01 ปีที่แล้ว

    Good video, and ur voice is so relaxing sir.

  • @officemishler3364
    @officemishler3364 ปีที่แล้ว

    Video and audio quality are ridiculously good. Need S Dubs in the highest def possible

  • @pukey3226
    @pukey3226 ปีที่แล้ว

    Thanks Shawn

  • @matadorso
    @matadorso ปีที่แล้ว +1

    Cool video 👌

  • @henrikchristensen65688
    @henrikchristensen65688 ปีที่แล้ว

    I love your short but very informative videos! You just got a new subscriber!

  • @FireIn8
    @FireIn8 2 หลายเดือนก่อน

    I have need for a collection of records (immutable) to do some quick lookups of various properties (no changes). Which might be a faster collection to use, and do collections affect the records they hold?

  • @williamliu8985
    @williamliu8985 ปีที่แล้ว +2

    Hi Shawn, could you give a video talking about the "readonly record struct"? The name seems a bit wired, and it is rarely mentioned. What is its application scenario?

    • @fifty-plus
      @fifty-plus ปีที่แล้ว

      It's well documented on the Microsoft Learn site.

  • @JoeAnzalone
    @JoeAnzalone ปีที่แล้ว

    Thanks this was great.

  • @jameshancock
    @jameshancock ปีที่แล้ว +2

    What I really want to do is make all of my DTOs readonly record struct. But it doesn't work in a number of cases including [AsParameter].

    • @swildermuth
      @swildermuth  ปีที่แล้ว

      Not sure why that would be. Can you explain? I'm curious. Difference between struct and record is inheritance (though likely not that useful but good to know).

    • @jameshancock
      @jameshancock ปีที่แล้ว +1

      @@swildermuth One is on the heap the other isn't. Correct usage of structs instead of classes in high volume systems minimizes garbage collection.
      It's unfortunate that you can't inherit or at least say "implement identical properties" of a struct, but when using protobuf of messagepack or even graphql which generate DTOs from other formats, that doesn't really matter, and the perf improvement is pretty nuts.

  • @zaharivaklinov
    @zaharivaklinov ปีที่แล้ว +2

    Thanks for this!
    But what's your take on creating DTOs with records instead of with classes?

    • @jonathansaindon788
      @jonathansaindon788 ปีที่แล้ว +1

      This is probably a good fit, but to me the best use case for records is multithreading. Since you can’t edit them after the fact, it forces you to use them in a thread-safe manner.

  • @vidhyafernandez
    @vidhyafernandez 5 หลายเดือนก่อน

    How would a collection of objects work inside a record? Would it still be value type?

    • @swildermuth
      @swildermuth  5 หลายเดือนก่อน +1

      The container for the collection would be, but, obviously, not the objects themselves. So very little savings.

  • @LOL-cp6js
    @LOL-cp6js ปีที่แล้ว

    helpful👍🔥

  • @Elilmu_Nur
    @Elilmu_Nur 11 หลายเดือนก่อน +5

    You didn't really say why we should use records but thanks anyway.

    • @DamienSawyer
      @DamienSawyer 8 หลายเดือนก่อน

      At 7:55 he discusses when he uses them and (perhaps not exhaustively), why.

  • @peterstangelo8627
    @peterstangelo8627 ปีที่แล้ว

    Thanks for doing these insighful coding shorts. I noticed you use a shortcut to run the 'dotnet.exe' -was it a case of just making a copy and naming it 'n.exe' (in the same folder)?

    • @swildermuth
      @swildermuth  ปีที่แล้ว +1

      I got it from here: github.com/faniereynders/dotnet-sdk-helpers
      I renamed their dot.cmd to n.cmd

    • @peterstangelo8627
      @peterstangelo8627 ปีที่แล้ว

      @@swildermuth thank you

  • @ashimov1970
    @ashimov1970 ปีที่แล้ว

    how do I convert records into classes and vice versa?

  • @49riddickful
    @49riddickful ปีที่แล้ว

    How is that last bool method you wrote a part of the record above? What indicates that?

    • @swildermuth
      @swildermuth  ปีที่แล้ว

      It is just like a class declaration, you can add members (in this case a method) in the body of the record. I think that's what you're asking.

  • @robadobdob
    @robadobdob 9 หลายเดือนก่อน

    You can also mutate the record back onto itself e.g. product = product with { Price = 1.99m }
    I use this in Fluxor where the current state is passed into a reducer method.

    • @swildermuth
      @swildermuth  9 หลายเดือนก่อน

      Technically true, but it is new copy (a new reference). Right?

    • @robadobdob
      @robadobdob 9 หลายเดือนก่อน

      @@swildermuth yeah correct, it creates a new copy and assigns it back to the variable.

  • @Ben-nc9qw
    @Ben-nc9qw 8 หลายเดือนก่อน

    Are you the voice of Lester in GTA5?

  • @henrik3098
    @henrik3098 ปีที่แล้ว

    thanks my amercian bro

  • @0XAN
    @0XAN 8 หลายเดือนก่อน

    nice explication man; but nothing will be fast as a struct with aligned fields; my feeling is that C# is becoming a language for wankers sadly

    • @swildermuth
      @swildermuth  8 หลายเดือนก่อน

      That's aggressive. Sure, struct with aligned fields is faster, but to what end? Faster != better code in many cases. Sure, you're building a real-time system, every cycle counts; but the ability to do immutable data can make code better and more thread safe. If you're optimizing for cycles, but most of your work is on the network, your time savings is swallowed up in the aggregate. Let me guess, non-wankers write in Rust or Go?