C # Get variable name in string

hi could you help I need to get a variable (name) in string format. for example below, I need to display "test" as a string.

        int test = 69;
        //below does not work
        MessageBox.Show((string)test);
        // below works but displays the int value
        MessageBox.Show(test.ToString());

Thank you for your time.

Well, I have an enumeration:

    public enum ShipOrientation
    {
        North,
        East,
        South,
        West
    }

and I'm doing some direction-based processing, and if int North applies an enumerated north direction.

+3
source share
2 answers

not for variables, but if you push your variable into a class property, you can use expressions to achieve this.

good sample here

+5
source

This cannot be done normally - variable names are compiled in C #.

, , .

, , , - ; , - ?

+3

Source: https://habr.com/ru/post/1733881/


All Articles