C
C#16mo ago
zzzz

❔ how to use "with"

the name with does not exist in the current context
No description
32 Replies
Thinker
Thinker16mo ago
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
Human human = new Human() { Sex = "female" };
Human human = new Human() { Sex = "female" };
zzzz
zzzzOP16mo ago
No description
zzzz
zzzzOP16mo ago
"not a valid record type"
Thinker
Thinker16mo ago
Ah, yeah, with only works on records and structs
zzzz
zzzzOP16mo ago
aw 😦 unfortunate
Thinker
Thinker16mo ago
Although depends on what your Human class looks like, you could easily convert it to a record with little to no downsides
zzzz
zzzzOP16mo ago
No description
zzzz
zzzzOP16mo ago
class Human
{
private string? name;

public string? Name
{
get => name;
set { Console.WriteLine("{0} has decided their name is {1}!", name, value); name = value; }
}
public required string Sex { get; init; }
protected int Age { get; set; }

public void Grow()
{
Age++;
}
}
class Human
{
private string? name;

public string? Name
{
get => name;
set { Console.WriteLine("{0} has decided their name is {1}!", name, value); name = value; }
}
public required string Sex { get; init; }
protected int Age { get; set; }

public void Grow()
{
Age++;
}
}
Thinker
Thinker16mo ago
You could really just change class to record if you wanted to
zzzz
zzzzOP16mo ago
hm yeah, it just amounted to doing that
Thinker
Thinker16mo ago
Also while you're at it, use string interpolation instead of that overload of Console.WriteLine And also put the setter on two lines
set
{
Console.WriteLine($"{name} has decided their name is {value}!");
name = value;
}
set
{
Console.WriteLine($"{name} has decided their name is {value}!");
name = value;
}
zzzz
zzzzOP16mo ago
should it be record class instead of just record to ensure its a reference type?
No description
Thinker
Thinker16mo ago
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 class
zzzz
zzzzOP16mo ago
may i ask for a tldr for what a record is?
Thinker
Thinker16mo ago
A record is a regular class or struct but with automatic reference equality, a shorter syntax, and other nicities like with and automatic deconstruction.
zzzz
zzzzOP16mo ago
reference equality, meaning that if two records have the same data, they're considered equal?
Thinker
Thinker16mo ago
yep classes are by default compared by reference
zzzz
zzzzOP16mo ago
what about usual object methods like .Equals() etc
Thinker
Thinker16mo ago
those are overwritten by records
zzzz
zzzzOP16mo ago
oh, i assume same for ==
Thinker
Thinker16mo ago
yep
zzzz
zzzzOP16mo ago
before records were introduced what was the way to achieve this functionality? (also sorry for interrupting your explanations all the time)
Thinker
Thinker16mo ago
just writing everything out in its entirety records are pretty much just short-hand
public class Person : IEquatable<Person>
{
public string Name { get; init; }
public int Age { get; init; }

public Person(string name, int age)
{
Name = name;
Age = age;
}

public bool Equals(Person? other) =>
other is not null &&
Name == other.Name &&
Age == other.Age;

public override bool Equals(object? obj) =>
Equals(obj as Person);

public override int GetHashCode() =>
HashCode.Combine(Name, Age);

public override string ToString() =>
$"Person {{ Name = {Name}, Age = {Age} }}";

public void Deconstruct(out string name, out int age)
{
name = Name;
age = Age;
}

public static bool operator ==(Person a, Person b) => a.Equals(b);
public static bool operator !=(Person a, Person b) => !a.Equals(b);
}
public class Person : IEquatable<Person>
{
public string Name { get; init; }
public int Age { get; init; }

public Person(string name, int age)
{
Name = name;
Age = age;
}

public bool Equals(Person? other) =>
other is not null &&
Name == other.Name &&
Age == other.Age;

public override bool Equals(object? obj) =>
Equals(obj as Person);

public override int GetHashCode() =>
HashCode.Combine(Name, Age);

public override string ToString() =>
$"Person {{ Name = {Name}, Age = {Age} }}";

public void Deconstruct(out string name, out int age)
{
name = Name;
age = Age;
}

public static bool operator ==(Person a, Person b) => a.Equals(b);
public static bool operator !=(Person a, Person b) => !a.Equals(b);
}
This can be written as
public record Person(string Name, int Age);
public record Person(string Name, int Age);
zzzz
zzzzOP16mo ago
o: wow C# is amazing offtopic, but in your opinion, do you think humans are records? if two people have the same name, age
Thinker
Thinker16mo ago
Personally I would probably represent a human/person as a record, although depends on how complex you want the structure to be
zzzz
zzzzOP16mo ago
hmm.. what would be a quality humans have that makes them unique? their dna? lmao maybe i could give each human a hash
Thinker
Thinker16mo ago
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
zzzz
zzzzOP16mo ago
i see. i guess it would depend on what spec is
Thinker
Thinker16mo ago
yeah, usually referred to as "domain modeling"
zzzz
zzzzOP16mo ago
thank you!
Thinker
Thinker16mo ago
np catsip
Accord
Accord16mo ago
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.

Did you find this page helpful?