我与c#一起工作,我有使用C++和java的经验。我试图弄乱字典,但我真的无法得到这个工作。我有两个数组的数据类型必须是对象,后我将它们添加到两个不同的字典,即时尝试找到一个关键,但我不能让它进入if语句。这两个字典的声明是正确的字典1或dictionary2?另外我怎样才能找到一个价值的关键或由正确的词典中的价值或两者兼而有之。因为"1111"
每次都会被装箱到一个新的唯一对象C#处理字典
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'作为键,而不是“对象”? –