将字典分配给字典值

huangapple go评论73阅读模式
英文:

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&lt;int, int&gt;
{
  {1, 333},
  {7, 555},
  {23, 111},
  .....
}

Is it possible, to give dict1 as value of dict2?

var dict2 = new Dictionary &lt;int, int&gt;
{
  {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 &amp;&amp; dict1.key == key1 or key....n)
  {
    return dict1 value
  }

else if
{
  return dict2 value
}
else 
{
 return something else
}

答案1

得分: 1

你可以通过将字典类型更改为Dictionary&lt;int, object&gt;来实现,但这似乎是一件奇怪的事情。尤其如果这只是一个单一值的情况。我会考虑类似以下方式:

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&lt;int, object&gt;, 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 &lt;int, Dictionary&lt;int, int&gt;&gt;
{
  {0, new Dictionary&lt;int, int&gt;()}, 
  {1, new Dictionary&lt;int, int&gt;()}, 
};

Note that the outer dictionary has to be of type Dictionary&lt;int, Dictionary&lt;int,int&gt;&gt; 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 &lt;int, Dictionary&lt;int, int&gt;&gt;
{
  {0, new Dictionary&lt;int, int&gt;()}, 
  {1, new Dictionary&lt;int, int&gt;()}, 
};

Note that the outer dictionary has to be of type Dictionary&lt;int, Dictionary&lt;int,int&gt;&gt; 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.

huangapple
  • 本文由 发表于 2023年2月24日 01:07:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75548090.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定