C
C#9mo ago

❔ how to use "with"

the name with does not exist in the current context
No description
32 Replies
Thinker
Thinker9mo 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" };
fæ
9mo ago
No description
fæ
9mo ago
"not a valid record type"
Thinker
Thinker9mo ago
Ah, yeah, with only works on records and structs
fæ
9mo ago
aw 😦 unfortunate
Thinker
Thinker9mo ago
Although depends on what your Human class looks like, you could easily convert it to a record with little to no downsides
fæ
9mo ago
No description
fæ
9mo 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
Thinker9mo ago
You could really just change class to record if you wanted to
fæ
9mo ago
hm yeah, it just amounted to doing that
Thinker
Thinker9mo 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;
}
fæ
9mo ago
should it be record class instead of just record to ensure its a reference type?
No description
Thinker
Thinker9mo 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
fæ
9mo ago
may i ask for a tldr for what a record is?
Thinker
Thinker9mo 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.
fæ
9mo ago
reference equality, meaning that if two records have the same data, they're considered equal?
Thinker
Thinker9mo ago
yep classes are by default compared by reference
fæ
9mo ago
what about usual object methods like .Equals() etc
Thinker
Thinker9mo ago
those are overwritten by records
fæ
9mo ago
oh, i assume same for ==
Thinker
Thinker9mo ago
yep
fæ
9mo ago
before records were introduced what was the way to achieve this functionality? (also sorry for interrupting your explanations all the time)
Thinker
Thinker9mo 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);
fæ
9mo 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
Thinker9mo ago
Personally I would probably represent a human/person as a record, although depends on how complex you want the structure to be
fæ
9mo 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
Thinker9mo 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
fæ
9mo ago
i see. i guess it would depend on what spec is
Thinker
Thinker9mo ago
yeah, usually referred to as "domain modeling"
fæ
9mo ago
thank you!
Thinker
Thinker9mo ago
np catsip
Accord
Accord9mo 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.