字符串变量名 [英] String as variable name

查看:132
本文介绍了字符串变量名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中有可能使用一个字符串变量名一样结果
我有一个:?搜索

is it possible in C# to use a String like a variable name ?
I have got a:

String x = "matrix1_2";
Microsoft.VisualBasic.PowerPacks.RectangleShape y = ???;



结果
? - 应该有变量的名称... matrix1_2


??? - there should be the name of variable...matrix1_2

推荐答案

没有,你不能,这是没有意义的诚实有这样的功能。

No, you can't, and it makes no sense honestly to have a feature like that.

如果您需要与键和值动态分配一些数据,你可以使用的字典:

If you need to dynamically assign some data with key and value, you could use an dictionary:

Dictionary<string, RectangleShape> shapes = new Dictionary<string, RectangleShape>();
shapes.Add("matrix1_2", new RectangleShape( ... ));



然后,你可以简单地阅读变量像

Then you can simply read the "variable" like

shapes["matrix1_2"]

这篇关于字符串变量名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆