The Ultimate Guide to C# Records

The Ultimate Guide to C# Records

Zoran Horvat

1 год назад

25,143 Просмотров

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


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

@alfonsdeda8912
@alfonsdeda8912 - 18.01.2024 14:51

Hi Zoran, great video!
Why class shouldn't derive from another class like in your example with name?

Ответить
@mr_venik
@mr_venik - 18.01.2024 15:02

NonEmptyString record struct is nice, but in C# you can still use "default" keyword which would ignore parameterless constructor

Ответить
@MC_DarkMaster
@MC_DarkMaster - 18.01.2024 15:03

Can you make a video when to use record structs and when to use record classes?

Ответить
@nickbarton3191
@nickbarton3191 - 18.01.2024 15:12

Didn't even think about deriving records, didn't know about equality.
Glad to dump structs.
Always learn something from these videos.

Ответить
@curtmantle7486
@curtmantle7486 - 18.01.2024 15:33

Devs should watch this even if they think they understand records.

Ответить
@Mefator
@Mefator - 18.01.2024 15:43

You can also simply use readonly record struct to make its properties immutable by default.

Ответить
@logank.70
@logank.70 - 18.01.2024 17:10

Everyone should watch out when using the Primary Constructors feature. There is currently no way to mark the backing fields that it creates as readonly. Not a good feature to use in conjunction with Dependency Injection.

Ответить
@HuneSilver
@HuneSilver - 18.01.2024 17:25

thanks for the subtitles in english are very useful 🙂

Ответить
@barionlp
@barionlp - 18.01.2024 18:43

never heard that i should use record structs over normal structs
do you have any sources going deeper into that?

Ответить
@ghevisartor6242
@ghevisartor6242 - 18.01.2024 18:55

i'm dumb, since i'm using Blazor with JsInterop i had some objects that had to be passed between Blazor and javascript, I used a plain class with the IEquatable, when i could have just used record, i even knew about them. Ahh thanks for the video.

Ответить
@abj136
@abj136 - 18.01.2024 19:19

To be sure, I 100% understood what you were describing about records in the opening of this video. But I knew approximately 0% of the syntax you showed after, so it’s a good thing I did not skip the rest of this video per your advice.

Ответить
@TayyabMughalDIK
@TayyabMughalDIK - 18.01.2024 19:57

This is the best demo on records. It couldn't get better than this one. <3

Ответить
@alexhall840
@alexhall840 - 18.01.2024 22:06

Can you give some real world examples on using records. I am struggling to think of where I can use them in the business application I am working on.

Ответить
- 18.01.2024 22:12

Good stuff. I was looking for the video to discuss with dotnet team in the format: let's meet after a week and discuss it. Your topics so far were in the shelve labeled "this could be too difficult for junios and mids", but this one is flexible. Juniors and seniors will find topics to discuss. Thank you.

Ответить
@matheosmattsson2811
@matheosmattsson2811 - 18.01.2024 22:50

Love it. Used records a few times but always confused me what they really are tbh. Other than a quick way to create a small class (which is what I saw it as earlier). Now I'm kind of considering migrating a lot of classes to records.. Great video!

Ответить
@yufgyug3735
@yufgyug3735 - 18.01.2024 23:14

'make invalid state unrepresentable' - that is an excellent line

Ответить
@ДмитрийКондратенко-б5ь
@ДмитрийКондратенко-б5ь - 19.01.2024 00:10

Great video!

Ответить
@ДмитрийКондратенко-б5ь
@ДмитрийКондратенко-б5ь - 19.01.2024 00:17

I wondering if records can be used as entity models working with EF

Ответить
@cbgrasshopper
@cbgrasshopper - 19.01.2024 01:33

In a previous video, you recommended against using collections in records due to the issues with deep immutability. Based on this video, it seems you would not have the same concern if the record used frozen collections; is that accurate?

Ответить
@weluvmusicz
@weluvmusicz - 19.01.2024 01:47

In the record you can define required immutable properties like so: public required string FirstName { get; init; }

Ответить
@nickw656
@nickw656 - 19.01.2024 04:48

The later `record struct` makes the properties mutable by default. Does this seem like a backward trend?

Ответить
@cloudycloudlet7091
@cloudycloudlet7091 - 19.01.2024 16:05

Discord link is not working anymore :(

Ответить
@1992jamo
@1992jamo - 19.01.2024 21:54

Your syntax is so absolutely alien to me always, but I find your videos very valuable.
Things like var (firstName1, _) = samuelL; do not even look like C# to me. What is the variable here called?

Ответить
@Sydra.
@Sydra. - 19.01.2024 23:27

Too awesome!

Ответить
@williamturns1647
@williamturns1647 - 25.01.2024 08:19

This was a succinct deep-dive into records. Could you kindly make one on pattern matching, too? There are countless ways to use them and it doesn't come naturally to think in pattern-matching style after years of using verbose conditions.

Ответить
@halorx9863
@halorx9863 - 28.01.2024 05:24

Will you do F# somedays?

Ответить
@edkachalov
@edkachalov - 31.01.2024 20:42

C# invented record from Pascal

Ответить
@kinglygaurav
@kinglygaurav - 01.02.2024 03:19

Besides learning records, if anyone wants to learn effective communication, watch this! <3

Ответить
@slowjocrow6451
@slowjocrow6451 - 01.02.2024 11:32

It seems most objects I deal with have an Id. Is a record still suitable? It doesn't matter the equality of the other properties, only Id. And what about objects with many properties? Is a record slower?

Ответить
@MC_DarkMaster
@MC_DarkMaster - 03.02.2024 17:28

Would that be an candidate for record struct? If it does make sense how would a record struct look like?
public sealed class DateTimeRange
{
public DateTime Start { get; private set; }
public DateTime End { get; private set; }

public DateTimeRange(DateTime start, DateTime end)
{
EnsureStartIsBeforeEnd(Start, End);
Start = start;
End = end;
}

static void EnsureStartIsBeforeEnd(DateTime start, DateTime end)
{
if (start > end)
{
throw new DateTimeRangeException("The endtime must not be before the starttime");
}
}
}

Ответить
@kennyhendricks4293
@kennyhendricks4293 - 06.02.2024 19:15

Damn!!! I love this, I generally use it in DTOs scenario

Ответить
@Lonchanick
@Lonchanick - 08.02.2024 08:24

Enough material for at least 10 videos :) Nice!

Ответить
@averyrealtree545
@averyrealtree545 - 17.02.2024 15:38

What a wonderful guide! I love your concise and clear communication

Ответить
@mkama-u5p
@mkama-u5p - 22.02.2024 18:34

I would love to use records more but I have one doubt. Even if I just store values that are immutable, I often initialise SOME (but not all) values during creation and update OTHER later. Records don't seem to be in favour in that situation. Or maybe I can do RecordType x = x with {...} to create updated and in fact replace the old object? [I am rather a beginner;)]

Ответить
@ДинаЛинская
@ДинаЛинская - 11.04.2024 18:35

Excellent, full and deep explanation, thanks a lot 🎉

Ответить
@mahmmoudkinawy2783
@mahmmoudkinawy2783 - 05.05.2024 21:17

thank you! excellent explanations

Ответить
@zo1dberg
@zo1dberg - 11.05.2024 07:30

Great vid and love your accent and storytelling!

Ответить
@mohammadrezajavidpoor8307
@mohammadrezajavidpoor8307 - 14.05.2024 13:16

You Speak too bad and your accent is a disaster that left the video at minute 2

Ответить
@adollphus
@adollphus - 24.06.2024 21:08

Great content, thanks Zoran.

Ответить
@gard2054
@gard2054 - 17.07.2024 22:53

The best 13 min ever. Amazing explanation

Ответить
@RezaPouyaSenDev
@RezaPouyaSenDev - 19.07.2024 12:15

It was very good explanations. Thank you

Ответить
@marcotroster8247
@marcotroster8247 - 13.08.2024 22:21

Maybe I'm old school, but I don't like black magic. There's too much implicit behavior to reason about.

It's a bit like reflection. Very powerful feature, but you'll never know how a change will impact the calling scope.

Ответить
@lluism200
@lluism200 - 21.08.2024 19:49

I really like the looks of records but how do you manage using them as value objects when inserting data to a database where an identity is required?

Ответить
@lolyasuo1235
@lolyasuo1235 - 24.11.2024 13:28

Records have its place but I'm disappointed to see the community once again (the first time with minimal APIs) trying to re-invent classes. I mean people trying to use class features using records... For example: trying to have properties within records and even worse logic (any logic, validations, overrides etc).

Ответить
@milosmrdovic7233
@milosmrdovic7233 - 09.12.2024 23:15

Really useful video! Thanks, Zoran.

However, there's one significant problem with your NonEmptyString implementation. The way you implemented the validation on it does not prevent someone from making a non-zero length array of NonEmptyStrings, using the "with { Value = null }" expression, or even worse, simply forgetting to initialize a NonEmptyString field. In all these cases your parameterless constructor that throws will not be called. Say, for example, you have a Person class with NonEmptyString Name property and you forget to initialize it - the Value of the Person's Name will end up being null. This behavior defeats the purpose of having the NonEmptyString type in the first place.

If you want to enforce validation of your members so they don't end up with default values, you must use the standard reference-type record. Even then, you must be careful with auto-implemented init properties which can be set to whatever using the "with" expression.

Ответить
@osman3404
@osman3404 - 21.12.2024 00:41

Would you also use record struct to model money types in retail domains like ListPrice, CostPrice, Total, GrossProfit etc….?

Ответить
@franciscohenriquez6121
@franciscohenriquez6121 - 23.12.2024 17:46

Interesting video Sir, Is there any way to use DataAnnotations using record types as DTO ? if so, would that affect the immutability of that object ?

Ответить