How to create variables in C# in the same way as in PHP?

Asked

Viewed 96 times

3

You can use variable variables or something similar in C# as in PHP for example?

$example = "etc..";
$var = "example";

// variáveis variáveis
echo $$var; // etc...
echo ${$var}; // etc...

If not possible, why? The typing, structure, etc...does not allow or simply because it does not have?

1 answer

5


This is already a huge gambit in PHP. In C# it is even worse because it is a language that encourages solving everything at compile time.

Almost always (99+%) that someone does this is actually wanting to use one array or a table hash (map, dictionary, array associative).

When it is not one of these things one is wanting to do something wrong and has no notion of it or has some other mechanism that suits better.

One such mechanism would be run-time compilation, which is rarely needed, and if so, few people can safely do.

It does not have a specific mechanism because it does not need and because it should not exist in any language. So you can get the expected result, but not with this mechanism.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.