根据另一个变量命名变量

-8 c# variables

很抱歉,如果这是重复的,但我找不到我需要的东西(超过30分钟搜索网页)

我想用其他变量创建名称变量(字符串)是否有可能在c#中执行此操作?我是c#的新手.

Oli*_*lin 5

不,你不能,老实说拥有这样的功能毫无意义.

如果需要,可以使用字典,其中键可以是字符串:

 Dictionary<string, RectangleShape> shapes = new Dictionary<string, RectangleShape>();
    shapes.Add("nameTheKey", new RectangleShape( ... ));
Run Code Online (Sandbox Code Playgroud)

然后你可以简单地阅读"变量"之类的

shapes["nameTheKey"]
Run Code Online (Sandbox Code Playgroud)