C#C
C#3y ago
Binto86

✅ Get instance name inside of class

So i have class like this:
private class CliFlag
{
  public object Value { get; }
  public CliFlag(object value)
  {
    this.Value = value;
  }
}

and i want to create
ToString
that will return
-[nameOfTheCliFlag] [valueOfTheFlag]
like this
var flag = new CliFlag("x");
flag.ToString(); // -flag x

however i can't find o way to get the name of the current instance inside of the class, i thought about
nameof(this)
, but that doesn't work
is there any way how to get this working without passing the name of the flag from outside?
Was this page helpful?