热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何在C#中使用动态名称创建变量?-HowtocreatevariableswithdynamicnamesinC#?

Iwanttocreateavarinaforloop,e.g.我想在for循环中创建一个var,例如for(inti;i<10;i++){strin

I want to create a var in a for loop, e.g.

我想在for循环中创建一个var,例如

for(int i; i<=10;i++)
{
    string s+i = "abc";
}

This should create variables s0, s1, s2... to s10.

这应该创建变量s0,s1,s2 ...到s10。

6 个解决方案

#1


You probably want to use an array. I don't know exactly how they work in c# (I'm a Java man), but something like this should do it:

您可能想要使用数组。我不确切知道它们是如何在c#中工作的(我是一个Java人),但是这样的事情应该这样做:

string[] s = new string[10];
for (int i; i<10; i++)
{
    s[i] = "abc";
}

And read http://msdn.microsoft.com/en-us/library/aa288453(VS.71).aspx

并阅读http://msdn.microsoft.com/en-us/library/aa288453(VS.71).aspx

#2


Your first example wouldn't work in any language as you are trying to redefine the variable "i". It's an int in the loop control, but a string in the body of the loop.

当您尝试重新定义变量“i”时,您的第一个示例将无法使用任何语言。它是循环控件中的int,但是循环体中的字符串。

Based on your updated question the easiest solution is to use an array (in C#):

根据您更新的问题,最简单的解决方案是使用数组(在C#中):

string[] s = new string[10];
for (int i; i<10; i++)
{
    s[i] = "abc";
}

#3


Obviously, this is highly dependent on the language. In most languages, it's flat-out impossible. In Javascript, in a browser, the following works:

显然,这高度依赖于语言。在大多数语言中,它是不可能的。在Javascript中,在浏览器中,以下工作原理:

for (var i = 0; i<10 ; i++) { window["sq"+i] = i * i; }

Now the variable sq3, for example, is set to 9.

现在,变量sq3例如设置为9。

#4


You may use dictionary. Key - dynamic name of object Value - object

你可以使用字典。键 - 对象的动态名称值 - 对象

        Dictionary dictiOnary= new Dictionary();
        for (int i = 0; i <= 10; i++)
        {
            //create name
            string name = String.Format("s{0}", i);
            //check name
            if (dictionary.ContainsKey(name))
            {
                dictionary[name] = i.ToString();
            }
            else
            {
                dictionary.Add(name, i.ToString());
            }
        }
        //Simple test
        foreach (KeyValuePair kvp in dictionary)
        {
            Console.WriteLine(String.Format("Key: {0} - Value: {1}", kvp.Key, kvp.Value));
        }

Output:

Key: s0 - Value: 0
Key: s1 - Value: 1
Key: s2 - Value: 2
Key: s3 - Value: 3 
Key: s4 - Value: 4
Key: s5 - Value: 5
Key: s6 - Value: 6
Key: s7 - Value: 7
Key: s8 - Value: 8
Key: s9 - Value: 9
Key: s10 - Value: 10

#5


Use some sort of eval if it is available in the language.

如果可以使用该语言,请使用某种eval。

#6


This depends on the language.

这取决于语言。

Commonly when people want to do this, the correct thing is to use a data structure such as a hash table / dictionary / map that stores key names and associated values.

通常,当人们想要这样做时,正确的做法是使用数据结构,例如存储键名和关联值的哈希表/字典/映射。


推荐阅读
author-avatar
1257523034_627418
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有