© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
6 replies
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;
  }
}
private class CliFlag
{
  public object Value { get; }
  public CliFlag(object value)
  {
    this.Value = value;
  }
}

and i want to create
ToString
ToString
that will return
-[nameOfTheCliFlag] [valueOfTheFlag]
-[nameOfTheCliFlag] [valueOfTheFlag]
like this
var flag = new CliFlag("x");
flag.ToString(); // -flag x
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)
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?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

✅ Class instance reference
C#CC# / help
11mo ago
❔ class instance creation
C#CC# / help
4y ago
accessing an instance of a class in another class
C#CC# / help
3y ago
❔ accessing object from a class inside of another class
C#CC# / help
3y ago