Let's imagine the situation:
Cat Tomas = new Cat(); Cat Daryl = Tomas;
In this case, both the Tomas
and Daryl
variables refer to the same object.
What value should return if we Tomas.Name
to Tomas.Name
and what if we Daryl.Name
to Daryl.Name
? How in theory should the language determine from which name the method call was made which returns the name?
My guess in this case is that it is possible to make initialization of the variable name mandatory. Like
Cat Tomas = new Cat("Tomas");
This does not solve the problem, but it works in some cases. (in the example above, of course, will not work).
...........
Purely in theory, you can come up with something with glassrays. But not the fact that it will work in the release build.
...........
Purely in theory, it is possible to come up with something with reflection. But not the fact that it will always work as it should and does not produce false results, as in the example above.
In general, I strongly advise not to do so at all in principle. Use nameof()
in places where you need it. This is guaranteed not to bring problems.
And also, most likely, you are moving in the wrong direction and have come up with the wrong solution to your task.
nameof()
- dmeptynameof
function, though in more sophisticated versions, the reflection will save you. - nick_n_a