【发布时间】:2017-02-13 03:08:50
【问题描述】:
我是使用 c# 的新手,我有使用 c++ 和 java 的经验。我试图弄乱字典,但我真的不能让它工作。我有两个数组,数据类型必须是对象,在我将它们添加到两个不同的字典后,我试图在其中找到一个键,但我无法让它进入 if 语句。字典的两个声明中哪个是正确的 dictionary1还是字典2?另外,我如何通过键或通过正确字典中的值或两者找到键来查找值。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Practice_With_Dictionaries
{
class Program
{
static void Main(string[] args)
{
object[] array1 = new object[5];
array1[0] = "1111";
array1[1] = "2222";
array1[2] = "3333";
array1[3] = "4444";
array1[4] = "5555";
object[] speed = new object[5];
speed[0] = 1;
speed[1] = 2;
speed[2] = 3;
speed[3] = 4;
speed[4] = 5;
object[] keys = new object[1];
keys[0] = (object[])array1;
object[] speedTable = new object[1];
speedTable[0] = (object[])speed;
Dictionary<object, object> dictionary1 = new Dictionary<object, object>();
Dictionary<object[], object[]> dictionary2 = new Dictionary<object[], object[]>();
dictionary1.Add(keys, speedTable);
dictionary2.Add(keys, speedTable);
if (dictionary1.ContainsKey((object)"1111"))
{
var method = 1;
}
if (dictionary2.ContainsKey(array1))
{
var method = 2;
}
}
}
}
【问题讨论】:
-
也许您应该考虑使用
Int32或String作为键,而不是Object?
标签: c# object dictionary c#-4.0