I'd like to use the following C#6 code
var joe = new Self();
Console.WriteLine(joe);
... and get the following output:
joe
The following attempt
class Self {
public string Name { get; set; } = nameof(this);
public override string ToString() {
return Name;
}
}
fails as nameof
cannot be applied to this
. Is it there a workaround for this problem?
EDIT. The scenario I'm working with assures that no two references point to the same Self
object.
var joe = new Self(); var jack = joe;
. ShouldName
return"joe"
,"jack"
, or both? – Spakenameof(joe)
– AircrewmanReferenceEquals
,Equals
andGetHashCode
methods, maybe with a custom implementation (for exampleEquals
can check anId
field/property on the two inspected instances). Your idea of usingnameof
to achieve such a goal is very strange... – Brotherlythis.GetType().Name
? – Debt