How to set value by call variable name from string c#

前端 未结 1 1955
春和景丽
春和景丽 2021-01-29 04:03

Now I\'m new for c# development.

I have 100 data from Array and also have 100 variables.

How can I match 100 data with 100 variables?

for example

相关标签:
1条回答
  • 2021-01-29 04:43

    Here is an example of doing what you are asking for on the fly with a System.Collections.Generic.Dictionary, all keys in a dictionary must be unique, but since you are appending 1 to each key in your loop this will suffice:

    Dictionary<string, int> myKeyValues = new Dictionary<string, int>();
    
    for(int count = 0 ; count < array.length; count++)
    {
        //Check to make sure our dictionary does not have key and if it doesn't add key
        if(!myKeyValues.ContainsKey("someKeyName" + count.ToString())
        {
             myKeyValues.Add("someKeyName" + count.ToString(), count);
        }
        else
        {
            //If we already have this key, overwrite, shouldn't happen as you are appending a new int value to key each iteration
            myKeyValues["someKeyName" + count.ToString()] = count;
        }
    }
    
    0 讨论(0)
提交回复
热议问题