-2

The basic goal is to write a function as such:

int myInt = 5;
PrettyPrint(myVar);

//in some helper class
public static void PrettyPrint<T>(T var) {
    Console.WriteLine(/* ??? */ + ": " + var); //output: "myInt: 5"
}

I know we can do nameof(myVar) inline which would return "myVar", however, is it possible to abstract that into a function that lives somewhere else? If we do nameof in PrettyPrint, in this case it would return "var" and not the original name.

Now, I know you're thinking "just pass in a string for the name", but ultimately I'd like the following:

public static void PrettyPrintMany<T>(params T[] vars) {
    string s = "";
    foreach (T v in vars) {
        s += <original name> + ": " + t + " | ";
    }
    Console.WriteLine(s); 
}

int myInt = 5;
string myStr = cheese;
float myFloat = 3.14f;

PrettyPrintMany(v1, v2, v3); //"myInt: 5 | myStr: cheese | myFloat: 3.14 | "

I feel like this is not possible, but maybe there is some clever hackery that can be done?

puzzl
  • 833
  • 9
  • 19
  • 5
    Not possible, definitely. I can hardly imagine why would anyone need that. – Wiktor Zychla Jan 11 '21 at 18:52
  • 3
    Not only that, but also that "original" variable name may not even exists. For example, when you call with a constant value or with an expression. – Alejandro Jan 11 '21 at 18:53
  • 4
    With a sacrifice to readability, you can pass in an `Expression` instead of the value, there you have access to that chunk of code, including the variable. You'll end with something like this `PrettyPrint(()=>myfloat);`, though. And performance WILL suffer. – Alejandro Jan 11 '21 at 18:56
  • 1
    The best hack I can come up with is to get the first parent frame in the stack trace, get its file name and line number, look at the source file and parse it somehow. I don't recommend this, but it could technically work for some calls and only with the debug flag set. But you have to ask yourself some questions, such as what it should happen if you call the method with the direct value instead of a variable – ShamPooSham Jan 11 '21 at 19:35
  • I need it as a shorthand helper function; it's right there at the end of the code: `PrettyPrintMany(v1, v2, v3);` That's a nice, compact way of knocking out readable logs for debugging. The alternative is `PrettyPrintMany(v1, nameof(v1), v2, nameof(v2), v3, nameof(v3));` which is a lot more typing. Pretty simple. – puzzl Jan 11 '21 at 21:23
  • @DavidL is the function of StackOverflow to explain why we need to ask questions, or to ask for answers to questions regardless of why? – puzzl Jan 11 '21 at 21:29
  • 1
    @puzzl why tends to shape how. There could be any number of answers to a question, but there is usually a *best* answer that is directly shaped based on *why* you need to accomplish something. – David L Jan 11 '21 at 21:55

2 Answers2

1

The short answer is you cannot! the only way to have the variable name without using magic strings is using nameof which produces a constant string at compile time and not at runtime: so this:

var hello = "there!";
Console.WriteLine(nameof(hello));

will compile to this

var hello = "there!";
Console.WriteLine("hello");
Benzara Tahar
  • 2,058
  • 1
  • 17
  • 21
  • Hmm, given that, it seems like it should be doable with a preprocessor #define-style setup, but there is no #define in c# I guess? – puzzl Jan 11 '21 at 21:26
  • Given that nameof is a compile-time expression, is it not possible to write our own compile-time expressions? – puzzl Jan 11 '21 at 21:32
-2

i am not much of a C++ developer but you can pass a sturcture and inside structure your variable and that way you can have the variable name exactly as it was sent

Fahad Tahir
  • 112
  • 1
  • 8