rf1 is just a variable name, so even if you can do it, it will not be a class method. In the end, you could:
RefName rf1 = new RefName(); RefName rf2 = rf1;
it is the same instance ; What should rf1.outRefName() produce? No, I donβt think you can do it. In C # there are some hacker ways to do this (involving captured variables and checking the reflection or expression tree), but again - you get the variable name - nothing to do with the object. A better approach here might be to give your class a Name member and initialize the name in the constructor:
RefName rf1 = new RefName("myname");
Here, the name of the object is "myname". Not rf1 , which is the name of the variable.
source share