英文:
How to give Dictionary to the Dictionary value
问题
这是您提供的代码片段的翻译:
我有一个有趣的问题。是否有人知道我们是否可以给字典的值另一个字典?例如:
var dict1 = new Dictionary<int, int>
{
{1, 333},
{7, 555},
{23, 111},
......
}
是否可能将dict1
作为dict2
的值?
var dict2 = new Dictionary<int, int>
{
{0, dict1}, // 可能吗?如何做?还有其他方法吗?
{1, 565},
{2, 5286},
{3, 62836},
....
}
实际上,将有等于两个字典的键的变量同时出现的情况,例如:
var str = 0;
var str2 = 7;
返回值将是555;
或者:
var str = 0;
var str2 = 1;
返回值将是333;
我想这样返回:如何做?
if (dict2.key == 0 && dict1.key == key1或key....n)
{
返回dict1的值
}
else if
{
返回dict2的值
}
else
{
返回其他值
}
英文:
I have an interesting question. Does anyone know if we could give another Dictionary to the Dictionary's Value? Example:
var dict1 = new Dictionary<int, int>
{
{1, 333},
{7, 555},
{23, 111},
.....
}
Is it possible, to give dict1 as value of dict2?
var dict2 = new Dictionary <int, int>
{
{0, dict1}, //possible? How to do? any other ways?
{1, 565},
{2, 5286},
{3, 62836},
....
}
Actually there will be variables which equals to keys of both Dictionaries and comes same time example:
var str = 0;
var str2 = 7;
Return will be 555;
or:
var str = 0;
var str2 = 1;
return will be 333;
I would like to return like this: How to do it?
if (dict2.key == 0 && dict1.key == key1 or key....n)
{
return dict1 value
}
else if
{
return dict2 value
}
else
{
return something else
}
答案1
得分: 1
你可以通过将字典类型更改为Dictionary<int, object>
来实现,但这似乎是一件奇怪的事情。尤其如果这只是一个单一值的情况。我会考虑类似以下方式:
if (key == 0)
otherDict.GetValueOrDefault(someOtherKey, out value);
else
dict.GetValueOrDefault(key, out value);
英文:
You technically could by changing the Dictionary type to a Dictionary<int, object>
, but it seems an odd thing to do. Especially if this is a one-value thing. I would consider something like:
if (key == 0)
otherDict.GetValueOrDefault(someOtherKey, out value);
else
dict.GetValueOrDefault(key, out value);
答案2
得分: 0
Just like you would create a Dictionary instance for a regular variable. With new
.
var dict = new Dictionary <int, Dictionary<int, int>>
{
{0, new Dictionary<int, int>()},
{1, new Dictionary<int, int>()},
};
Note that the outer dictionary has to be of type Dictionary<int, Dictionary<int,int>>
because you cannot have some values be of one type and others be of another type. If that is your goal it's best to define a custom class which has an int as well as an optional Dictionary member as well as some logic that tells which of the two is currently in use.
Using "object" is usually not recommended as you fully lose type safety with that.
英文:
Just like you would create a Dictionary instance for a regular variable. With new
.
var dict = new Dictionary <int, Dictionary<int, int>>
{
{0, new Dictionary<int, int>()},
{1, new Dictionary<int, int>()},
};
Note that the outer dictionary has to be of type Dictionary<int, Dictionary<int,int>>
because you cannot have some values be of one type and others be of another type. If that is your goal it's best to define a custom class which has an int as well as an optional Dictionary member as well as some logic that tells which of the two is currently in use.
Using "object" is usually not recommended as you fully loose type safety with that.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论