How to Gets the name of the instance?

兰树豪 381 Reputation points
2023-06-16T08:00:54.84+00:00

Let's say we have a class:

Class Myclass

Class Myclass

{    
    public string MyINstranceName()
   {
        return .... // how to get the instrance name here?  here will be "myclass1"   
                          in this demo

   }
}

// Create an instance

Myclass myclass1 = new Myclass();


}

// Create an instance

Developer technologies Windows Presentation Foundation
Developer technologies .NET Other
Developer technologies Visual Studio Other
Developer technologies C#
{count} votes

3 answers

Sort by: Most helpful
  1. Viorel 122.6K Reputation points
    2023-06-16T08:15:21.66+00:00

    Probably you should consider an alternative:

    class MyClass
    {
        public readonly string File;
        public readonly int Line;
    
        public MyClass( [CallerFilePath] string file = null, [CallerLineNumber] int line = 0 ) 
        {
            File = file;
            Line = line;
        }
    
        public string MyInstanceLocation( )
        {
            return Path.GetFileName( File ) + " " + Line;
        }
    }
    
    . . .
    
    MyClass myclass1 = new MyClass();
    

    This offers the file and the line of the instantiation, which is useful too.

    0 comments No comments

  2. Hui Liu-MSFT 48,676 Reputation points Microsoft External Staff
    2023-06-16T09:01:15.7+00:00

    Hi,@兰树豪. Welcome Microsoft Q&A. In C#, the instance name is not directly accessible within the class itself. The instance name is a reference to the object, and it is not stored as a property or value within the object.

    If you want to get a custom name or identifier for each instance of the MyClass class, you would need to pass it explicitly when creating the instance. Here's an example:

    public class MyClass
    {
        public string InstanceName { get; set; }
    
        public MyClass(string instanceName)
        {
            InstanceName = instanceName;
        }
    
        public string GetInstanceName()
        {
            return InstanceName;
        }
    }
    
    
    

    create an instance of MyClass:

    MyClass myClass1 = new MyClass("myclass1");
    string instanceName = myClass1.GetInstanceName();  // Returns "myclass1"
    
    
    

    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  3. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2023-06-20T20:39:03.3466667+00:00

    you are not asking the instance name, but rather the name of variable that references it. there can be any number of references.

    Myclass myclass1 = new Myclass();
    Myclass myclass2 = myclass1;      // references the same instance as myclass1
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.