You’re talking about local variables and these are just nicknames for memory addresses totally unnecessary for execution, it’s just an abstraction to facilitate the programmer’s understanding.
After compilation there is no variable name (local, in languages/implementations that are 100% native and static, even non-local symbols can be dropped), so there is nothing to access. If the exercise asks for exactly this, and I don’t think it does, which is misinterpretation, the exercise is flawed and makes no sense. If it’s not a drill, there’s something very wrong.
What you can do is put a parameter in the method and pass the name as string
as argument for the method. Even this makes no sense.
Remembering that you always know the name of variables local during development, so it makes no sense to create a code to find out what the name is.
Even the nameof()
only exists as a protective measure to prevent code from de-synchronizing with the text used, so if you rename the variable, compile the nameof()
will fail and you can change manually agreeing that now the other name is correct. When you use the string
with the variable name, if you had a "x"
, and the variable changes to y
, the code gets "x"
and may not be noticed. In practice this operator does nothing real in the application.
It makes no sense to know the name of the variable that was used to call the method, it has zero benefits in it. Nor should it have any. The only languages that make sense are the ones that have dynamic scope, which is one of the most idiotic ideas ever created in computing, and I can talk about that because I’ve worked with language like that, but luckily today it’s optional, and even when it was mandatory to do so it was better to pretend that it could not even take advantage of this.
So you don’t have to go by this name. And if you really needed to, you should justify it. Who knows is an XY problem (see next if you are reading this during Cup :D ). This method getName()
is simply unnecessary and a programming error. So it may have some relevance, yet weird:
public class A {
public A() {
var nameToWrite = "thisdoesntmatter";
B.WriteName(nameof(nameToWrite));
}
}
public class B {
public static WriteName(string name) => WriteLine(name));
}
I put in the Github for future reference.
Note that I even changed the name of the method to make sense, and it should only exist as abstraction.
This is the same thing:
public A() {
var nameToWrite = "thisdoesntmatter";
WriteLine("nameToWrite"); //o nameof() daria mais proteção contra alteração desavisada
}
Hi, this is the website of stackoverflow in Portuguese, translate your question or also ask on the site in English
– Ricardo Pontual
What is the reason for this? What do you really need?
– Gabriel Heming
You want to take the variable value or just the variable name?
– Maycon F. Castro
I don’t think Reflection this must be possible, there is no link with the variable in the other method, even passing by reference
– Ricardo Pontual
@Gabrielheming I am building a Generica class that interests me the name of the object directly past.
– Pedro Martins
@Mayconf.Castro This question is indicated in the text, I only care about the name of the variable that is passed and not the internal, nor values
– Pedro Martins
@Ricardopunctual Because, I have tried several ways and I can’t get to the name, even with reflecion and propertInfo. I can access the properties names of this object with Reflection but the name of this object I do not know.
– Pedro Martins
@Pedromartins pq not get the name so
var nomeVariavel = nameof(nameToWrite);
? Note that the name of the variables can and will probably change after compiled.– Omni
You will not get, because variable names are not relevant from an operating point of view. Only on language and/or performance limitation issues.
– Gabriel Heming
It should be in line with what @Gabrielheming commented, and so when you "decompile" a code the variable names are not necessarily the same in the original code.
– Ricardo Pontual
Note: the
nameof
solves the name at compile time (ie the name will be what you see and there is no resolution while running the application)– Omni
Apparently you want to go further. In addition to the excellent answers and comments: Take a look at the F#
– Tony
@Tony what the F# has to offer in this?
– Maniero
It offers more resources.
– Tony