DEV Community

Cover image for Where would you use the new C# 9 Record Type
klyse
klyse

Posted on

4 1

Where would you use the new C# 9 Record Type

I found a great post by wrijugh.

Wriju explains the new C# Record Type.

I am excited about this feature, but I'm not 100% sure when I should use it and when I should stick to a class. Mads Torgersen provided some insights there in his presentation on C# 9 during the .net conference but I'm actually more interested in the practical side.

How do you plan on using this new feature? Do you think there are issues one has to think about when one uses the new Record Type? Do you think Record Types should be used for deserialization of a json string? Are you going to migrate from classes to Record Types?

Let me know in the comments :)!

Top comments (5)

Collapse
 
gsferreira profile image
Guilherme Ferreira โ€ข

Hi!
Records will be really useful in cases where we are looking for immutable data. Examples:

  • DTOs
  • Events when working in event sourcing
  • Commands
  • ...

I recommend you to read this blog post (pre-records) by Jimmy Bogard when he explains the challenges of immutable classes in .net. Know, with records, it's embedded in the language.

jimmybogard.com/immutability-in-dtos/

Collapse
 
klyse profile image
klyse โ€ข

Hi,
thanks for the comment! I agree records can be great for DTOs and Commands (I'm also thinking about MediatR). As Jimmy Bogard mentioned in his blog post upon until now Immutable objects could not be serialized easily. Now that we have records they are immutable and can be serialized ๐ŸŽˆ. Here is a small example of the serialization (I couldn't believe it, I had to try it myself :D) dotnetfiddle.net/e8tSIN

Collapse
 
_hs_ profile image
HS โ€ข โ€ข Edited

Agree with that. Kind of like what I do with kotlin and java. Data stuff is data class (record in C# and java). Services and others are class so it can mutate itself. In Java I just make stuff final if it's not used by some naggy framework until records are stable

Collapse
 
klyse profile image
klyse โ€ข โ€ข Edited

Hi Ninja,
Thanks for the comment. What you said makes a lot of sense to me: if you need to move data from one to another use a record.
IMHO records could also become a bit of a pain, if not used correctly. Think about this:

public class Foo
{
    public string S1 {get;set;}
    public string S2 {get;set;}
}

record Bar(int i1, Foo f);
Enter fullscreen mode Exit fullscreen mode

I think records could be also really helpful to serialize and deserialize stuff (because it reduces the lines of code by a lot).

Collapse
 
peledzohar profile image
Zohar Peled โ€ข

I totally agree. IMHO, immutability should be the default and mutable properties should be used with caution. This also means I prefer having readonly collections whenever possible.

Sentry workshop image

Flaky tests got you down?

Learn how to merge your code without having to hit โ€œrerunโ€ every 5 minutes ๐Ÿ˜ฎโ€๐Ÿ’จ

Save your spot now.

๐Ÿ‘‹ Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay