2011-07-27 126 views
-2

我想將元素添加到以下內容。它不工作。我該怎麼做 ?試圖將元素添加到列表<String[,]> S = new List <String[,]>();

List<String[,]> S = new List<String[,]>(); 

爲了告訴你爲什麼我要嘗試這樣的事情; 我最初需要以下::

 String[,] s = new String[60,2] ; 
     s[0,0] = ".NET CLR LocksAndThreads"; 
     s[0,1] = "Contention Rate/sec"; 
     s[1,0] = "ASP.NET Applications"; 
     s[1,1] = "Requests Rejected"; 
     s[2,0] = "Memory"; 
     s[2,1] = "Available Mbytes"; 
     s[3,0] = "Process"; 
     s[3,1] = "Private Bytes"; 
     s[4,0] = "Network Interface"; 
     s[4,1] = "Bytes Received/sec"; 

但轉念一想,爲什麼不使用名單。所以請告訴我我做錯了什麼..

+6

「不工作」在嘗試排除故障時是完全沒有用的信息。可能想重新修改你的問題。 –

+0

首先,我不明白你是如何添加元素的。 – BoltClock

回答

6

您似乎想要將二維字符串數組轉換爲您的給定數據更有意義的東西。

看在你的二維數組的值,並基於這樣的事實,它是由2陣列的X,它可能更有意義使用字典:

Dictionary<string, string> S = new Dictionary<string, string> 
{ 
    { ".NET CLR LocksAndThreads", "Contention Rate/sec" }, 
    { "ASP.NET Applications", "Requests Rejected" }, 
    { "Memory", "Available Mbytes" }, 
    { "Process", "Private Bytes" }, 
    { "Network Interface", "Bytes Received/sec" } 
}; 
3

好像你想存儲雙串。如果每對中的第一個字符串是唯一的(我懷疑它是這樣),那麼Dictionary會爲你做這個。

例如

var dictionary = new Dictionary<string, string> 
    { 
     { "a", "x" }, 
     { "b", "y" }, 
    } 

如果每對中的第一個字符串是唯一的,那麼你可以使用的KeyValuePair的集合。

var list = new List<KeyValuePair<string, string>> 
    { 
     new KeyValuePair<string, string>("a", "x"), 
     new KeyValuePair<string, string>("b", "y"), 
    } 
+0

關於字符串/鍵的唯一性的好處... – BoltClock

相關問題