C
C#14mo ago

❔ how to use "with"

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