I stumbled upon this article, which is devoted to chips, which with high probability will be added to the new version of the language.

Caller Argument Expression

It turns out that the compiler can now be forced to get the name of the variable that was passed from the outside to the method?

    1 answer 1

    Yes, if the variable was passed. In general, it will be possible to get a string representation of the expression passed to the method.

    Focusing on the relevant proposal on Github. .

    If the method is declared like this:

    public static class Debug { public static void Assert(bool condition, [CallerArgumentExpression("condition")] string message = null); } 

    and is called like this:

     Debug.Assert(someBoolean); Debug.Assert(array != null); Debug.Assert(array.Length == 1); 

    , the compiler will substitute the value of the second argument:

     Debug.Assert(someBoolean, "someBoolean"); Debug.Assert(array != null, "array != null"); Debug.Assert(array.Length == 1, "array.Length == 1");