2017-04-18 43 views

回答

1

不,你不能。 C#是強類型編程語言。你可以做什麼,就是用字典例如,像這樣:

Dictionary<string, int> names = new Dictionary<string,int>(); 
    for (int i = 0; i < 10; i++) 
    { 
     names.Add(String.Format("name{0}", i.ToString()), i); 
    } 

    var xx1 = names["name1"]; 
    var xx2 = names["name2"]; 
    var xx3 = names["name3"]; 
0

如果我理解正確的話,你希望得到您的變量的名稱,然後根據迭代每個實例更改名稱,從而你想爲名稱添加一個數字值。你可以做到這一點,如下所示:

string testVariable = ""; 
int i = 1; 

// Get the name of your variable. This will result in "testVariable" as value. 
string nameOfTestVariable = nameof(testVariable); 

// Now add your integer to the name, but add it as a string. 
nameOfTestVariable += i.ToString(); 

//Result, value of nameOfTestVariable: "testVariable1" 

爲了讓您的數據類型變量代替:

int a = 0; 
int b = 1; 

string dataTypesOfVariables = a.GetType().Name;  
dataTypesOfVariables += b.GetType().Name; 

//Result, value of dataTypesOfVariables: "Int32Int32" 
+0

喜Jurjen, 上面的代碼工作正常 但我需要整數varibale不是字符串 –

+0

@SuneelSandara我不確定我是否理解,如果你想添加變量的名字,你總是會得到一個字符串(因爲我們在這裏討論的是名字,而不是整數值),或者如果你只是想要數值,你可以添加整數? – Jurjen

+0

我明白,我正在採取有關數據類型,我需要整數數據類型變量名稱 –

相關問題