How do I obtain an ID that allows me to tell difference instances of a class apart?

前端 未结 6 1887
谎友^
谎友^ 2021-02-02 13:30

Imagine I have a single class, with two instances:

MyClass a = new MyClass();
MyClass b = new MyClass();

MyClass has a method PrintUniqueInstan

6条回答
  •  太阳男子
    2021-02-02 14:14

    Could potentially use:

    ClassName + MethodName + this.GetHashCode();
    

    Although GetHashCode() does not guarantee a unique value, if its paired with the class name and method name, the likelihood of a clash reduces.

    Even if there is a clash, the only effect will be that it generates more warnings in the logs, which isn't a big deal.

提交回复
热议问题