2012-03-26 92 views
0

到目前爲止,我正在使用簡單數組來輸入並獲取必要的信息。輸入並從一個ConcurrentDictionary中獲取數組的值C#

第一個例子是以下幾點:

  // ===Example 1. Enter info //// 
      string[] testArr1 = null; 
      testArr1[0] = "ab"; 
      testArr1[1] = "2"; 
      // ===Example 1. GET info //// 
      string teeext = testArr1[0]; 
      // ======================= 

和第二個例子:

  // ====== Example 2. Enter info //// 
      string[][] testArr2 = null; 
      List<int> listTest = new List<int>(); 
      listTest.Add(1); 
      listTest.Add(3); 
      listTest.Add(7); 
      foreach (int listitem in listTest) 
      { 
       testArr2[listitem][0] = "yohoho"; 
      } 
      // ====== Example 2. Get info //// 
      string teeext2 = testArr2[0][0]; 
      // ======================= 

但現在我想分配一個標識號每個數組,所以我可以在一個ConcurrentDictionary中標識多個不同的數組

如何在字典中輸入和獲取信息數組?

看,我們有兩個標識符和兩點字典:

  decimal identifier1 = 254; 
      decimal identifier2 = 110; 
      ConcurrentDictionary<decimal, string[]> test1 = new ConcurrentDictionary<decimal, string[]>(); 
      ConcurrentDictionary<decimal, string[][]> test2 = new ConcurrentDictionary<decimal, string[][]>(); 

我想象這樣的事情:

//////////Example 1 
//To write info 
test1.TryAdd(identifier1)[0] = "a"; 
test1.TryAdd(identifier1)[1] = "b11"; 

test1.TryAdd(identifier2)[0] = "152"; 
//to get info 
string value1 = test1.TryGetValue(identifier1)[0]; 
string value1 = test1.TryGetValue(identifier2)[0]; 

//////////Example 2 
//To write info: no idea 
//to get info: no idea 

PS:上面的代碼不工作(因爲它是自made)..那麼什麼是通過一個ID在ConcurrentDictionary中向string []和string [] []輸入信息的正確方法? (並且把它弄出來)

+1

我很困惑。你試圖達到什麼目標? – zmbq 2012-03-26 20:34:39

+0

「我試圖給每個數組分配一個標識號,所以我可以在ConcurrentDictionary中標識不同的數組」 – Alex 2012-03-27 07:55:15

回答

0

鑑於你的聲明

decimal identifier1 = 254; 
decimal identifier2 = 110; 
ConcurrentDictionary<decimal, string[]> test1 = new ConcurrentDictionary<decimal, string[]>(); 
ConcurrentDictionary<decimal, string[][]> test2 = new ConcurrentDictionary<decimal, string[][]>(); 

你可以使用這樣的代碼

test1[identifier1] = new string[123]; 
    test1[identifier1][12] = "hello"; 

    test2[identifier2] = new string[10][]; 
    test2[identifier2][0] = new string[20]; 
    test2[identifier2][0][1] = "world"; 

輸入您的數據。以下是訪問您所輸入的數據的一個例子:

Console.WriteLine(test1[identifier1][12] + " " + test2[identifier2][0][1]); 

我必須說,雖然,這樣的代碼往往是相當令人費解和難以調試,尤其是對test2的情況。你確定你想使用鋸齒陣列嗎?

+0

「,我需要將數據存儲在某處..選擇是在INI文件,SQl數據庫和ConcurrentDictionary之間進行的。 – Alex 2012-03-27 07:54:24

相關問題