-4

I have an array

int[] arr1 = new int[] { 1, 2, 3 };
Console.WriteLine("How to print the text arr1 ?? ");

I need the array name i.e. 'arr1' in console output/Writeline, seems simple but need help.

Edit :

Output should be

arr1

NOT

1 2 3

Good to know, this is achieved in PHP too.

Manjuboyz
  • 6,978
  • 3
  • 21
  • 43
  • 7
    http://stackoverflow.com/questions/255312/how-to-get-a-variable-name-as-a-string-in-php http://stackoverflow.com/questions/716399/c-sharp-how-do-you-get-a-variables-name-as-it-was-physically-typed-in-its-dec – Andrew Oct 14 '15 at 03:58
  • Give me a valid reason before down voting. thanks. – Manjuboyz Oct 14 '15 at 04:20
  • because, there are already this questions and answers, you nedd just search – Backs Oct 14 '15 at 04:23
  • and multi language tags are not appreciated –  Oct 14 '15 at 04:30
  • Valid reasons: Bad tags (this is not at all related to a console application, and asking for implementations in two different languages leaves much to be desired, let alone writing a 2-in-1 question). No evidence whatsoever of you making even a slight attempt at achieving this yourself - you simply asked us to do something for you. Not how SO works. And finally, googling `C# print variable name` gives you two hits for SO questions, both of which have your solution. – Rob Oct 14 '15 at 04:48
  • So do you want to do this in php or c#? – Sweeper Oct 14 '15 at 04:59
  • Ok, I agree, i have used console application for testing purpose not to implement, i was with the conversation where above conversation, can be achieved or not, that made me to use console application nothing else, once of the reason i asked 'php' too because our team is also working on php and want to know, it can be achieved. – Manjuboyz Oct 14 '15 at 06:11

1 Answers1

0

If you declare/initialize variables in a far more complicated way you could...

echo "<pre>";
$var_name="Foo";  // naming the var "Foo"
$$var_name="Bar"; // declaring the var "Foo"

echo "value: ". $Foo . "<br><br>";

foreach(compact($var_name) as $key=>$value){ // refering to var name
    echo "name: " . $key . "<br><br>";  
}

echo "pair: ";
print_r(compact($var_name));
echo "<pre>" ; 

But we'd still need to get it through compact($var_name) instead of compact($Foo)

Julio Soares
  • 1,200
  • 8
  • 11