i have a statement
int A = 10,B=6,C=5;
and i want to write a print function such that i pass the int variable to it and it prints me the variable name and the value.
eg if i call print(A) it must return ‘A: 10’, and print (B) then it must return ‘B:6’
in short i want to know how can i access the name of the variable and print it to string in c#. DO i have to use reflection?
After reading the answers
Hi all, thanks for the suggestions provided. I shall try them out, however i wanted to know if it is at all possible in .NET 2.0? Nothing similar to
#define prt(x) std::cout << #x ' = '' << x << ''' << std::endl;
macro which is there in C/C++?
The only sensible way to do this would be to use the
ExpressionAPI; but that changes the code yet further…Note: if this is for debugging purposes, be sure to add
[Conditional('DEBUG')]to the method, as using a variable in this way changes the nature of the code in subtle ways.