32 Replies
That's not how you use
with
Remove the new Human()
after with
Although you don't really need with
here anyway since this just looks like a regular object initializer
"not a valid record type"
Ah, yeah,
with
only works on records and structsaw 😦 unfortunate
Although depends on what your
Human
class looks like, you could easily convert it to a record with little to no downsidesYou could really just change
class
to record
if you wanted tohm
yeah, it just amounted to doing that
Also while you're at it, use string interpolation instead of that overload of
Console.WriteLine
And also put the setter on two lines
should it be
record class
instead of just record
to ensure its a reference type?record
is always a reference type, but you can use record class
if you feel it makes it clearer
or if you use record structs a lot and you feel the need to clarify between what is a record struct and classmay i ask for a tldr for what a record is?
A record is a regular class or struct but with automatic reference equality, a shorter syntax, and other nicities like
with
and automatic deconstruction.reference equality, meaning that if two records have the same data, they're considered equal?
yep
classes are by default compared by reference
what about usual object methods like .Equals() etc
those are overwritten by records
oh, i assume same for ==
yep
before records were introduced what was the way to achieve this functionality?
(also sorry for interrupting your explanations all the time)
just writing everything out in its entirety
records are pretty much just short-hand
This can be written as
o: wow
C# is amazing
offtopic, but in your opinion, do you think humans are records?
if two people have the same name, age
Personally I would probably represent a human/person as a record, although depends on how complex you want the structure to be
hmm.. what would be a quality humans have that makes them unique? their dna? lmao
maybe i could give each human a hash
Yeah like in the programming sense you could probably assume two people are the same if they have the same values, although if you were to strive for a "complete" model of a human then yeah you'd probably compare by DNA or something
i see. i guess it would depend on what spec is
yeah, usually referred to as "domain modeling"
thank you!
np
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.