2012-03-26 88 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

相关问题