【问题标题】:entering and getting values from arrays in one ConcurrentDictionary with C#使用 C# 从一个 ConcurrentDictionary 中的数组输入和获取值
【发布时间】:2012-03-26 20:25:38
【问题描述】:

到目前为止,我一直在使用简单数组来输入和获取必要的信息。

第一个例子如下:

            // ===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

P.S.:上面的代码不起作用(因为它是自制的).. 那么通过 ID 在 ConcurrentDictionary 中的 string[] 和 string[][] 中输入信息的正确方法是什么? (并把它拿出来)

【问题讨论】:

  • 我很困惑。你想达到什么目的?
  • "我正在尝试为每个数组分配一个标识号,以便在 ConcurrentDictionary 中识别不同的数组"

标签: c# get enter concurrentdictionary


【解决方案1】:

鉴于你的声明

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 案例。您确定要使用交错数组吗?

【讨论】:

  • 好吧,我需要将数据存储在某个地方.. 选择是在 INI 文件、SQl 数据库和 ConcurrentDictionary 之间。
  • 写这样的值不是线程安全的,为什么要使用 ConcurrentDictionary?
猜你喜欢
  • 1970-01-01
  • 2012-04-09
  • 1970-01-01
  • 2012-10-13
  • 1970-01-01
  • 1970-01-01
  • 2016-04-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多