如何在 C# 中将字典初始化为 (key,value,value) 对
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1845741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to initialize dictionary as (key,value,value) pair in C#
提问by Royson
I want to store values as key,value,value pair. My data is of type
我想将值存储为键、值、值对。我的数据类型
Key -> int & both values -> ulong,
How to initialize & fetch values of such dictionary. I am using VS-2005.
如何初始化和获取此类字典的值。我正在使用 VS-2005。
If i use a class or struct then how do i fetch the values.
如果我使用类或结构,那么我如何获取值。
采纳答案by bniwredyc
Create a structure to store your values:
创建一个结构来存储您的值:
struct ValuePair
{
public ulong Value1;
public ulong Value2;
}
Dictionary initialization:
字典初始化:
Dictionary<int, ValuePair> dictionary = new Dictionary<int, ValuePair>();
Maybe List is enough, if you use int as key?
如果您使用 int 作为键,也许 List 就足够了?
List:
列表:
List<ValuePair> list = new List<ValuePair>();
ValuePair
can be added to the list
as following:
ValuePair
可以添加到list
以下内容中:
list.Add(new ValuePair { Value1 = 1, Value2 = 2 });
回答by Benny
maybe you have to define a class say class Pair to hold your two value, and use int as the key.
也许你必须定义一个类,比如 class Pair 来保存你的两个值,并使用 int 作为键。
回答by Konamiman
You can declare a class that stores both values and then use an ordinary dictionary. For example:
您可以声明一个存储这两个值的类,然后使用普通字典。例如:
class Values {
ulong Value1 {get;set;}
ulong Value2 {get;set;}
}
var theDictionary=new Dictionary<int, Values>;
theDictionary.Add(1, new Values {Value1=2, Value2=3});
回答by naspinski
This would be an option:
这将是一个选择:
Dictionary<int, KeyValuePair<ulong, ulong>> dictionary = new Dictionary<int, KeyValuePair<ulong, ulong>>();
If you want to add in a value: Key=1, Pair = {2,3}
如果要添加一个值:Key=1, Pair = {2,3}
dictionary.Add(1, new KeyValuePair<ulong, ulong>(2, 3));
If you want to retrieve those values:
如果要检索这些值:
var valuePair = dictionary[1];
ulong value1 = valuePair.Key;
ulong value2 = valuePair.Value;
Or simply:
或者干脆:
ulong value1 = dictionary[1].Key;
回答by Sune Rievers
I'm not sure I understand your question correctly, but if you want to store more than one value in the value part of Dictionary, you could do something like this:
我不确定我是否正确理解您的问题,但如果您想在 Dictionary 的值部分存储多个值,您可以执行以下操作:
var dic = new Dictionary<int,KeyValuePair<ulong,ulong>>();
You can use insert into the dictionary like this:
您可以像这样使用插入到字典中:
dic.Add(42, new KeyValuePair<ulong, ulong>(42, 42));
dic.Add(43, new KeyValuePair<ulong, ulong>(43, 43));
And fetch the values like so:
并像这样获取值:
foreach (var a in dic)
{
Console.WriteLine("Key: {0}, Value1: {1}, Value2: {2}",
a.Key, a.Value.Key, a.Value.Value);
}
回答by Adriaan Stander
You can make use of the KeyValuePair
您可以使用 KeyValuePair
Dictionary<int, KeyValuePair<ulong,ulong>> vals = new Dictionary<int, KeyValuePair<ulong, ulong>>();
回答by Grigory Bushuev
Look at Wintellect.PowerCollections Namespacethey have special structure Pair<(Of ) and collections to work with it or you'll need to code your own Pair type.
看看Wintellect.PowerCollections 命名空间,它们具有特殊的结构 Pair<(Of ) 和可以使用它的集合,否则您需要编写自己的 Pair 类型。
回答by Mark Rendle
Create a Tuple class, in the System namespace:
在 System 命名空间中创建一个 Tuple 类:
public class Tuple<T1,T2>
{
private readonly T1 _item1;
private readonly T2 _item2;
public Tuple(T1 item1, T2 item2)
{
this._item1 = item1;
this._item2 = item2;
}
public T1 Item1 { get { return _item1; } }
public T2 Item2 { get { return _item2; } }
}
And a static Tuple class with a Create method so you get type inference which is not available on constructors:
还有一个带有 Create 方法的静态 Tuple 类,因此您可以获得构造函数不可用的类型推断:
public static class Tuple
{
public static Tuple<T1, T2> Create<T1, T2>(T1 item1, T2 item2)
{
return new Tuple<T1, T2>(item1, item2);
}
}
Then, when you get onto .NET 4.0, you can delete these classes because they're in the Base Class Library (and are compatible with F# tuples!).
然后,当您使用 .NET 4.0 时,您可以删除这些类,因为它们位于基类库中(并且与 F# 元组兼容!)。
回答by Martin R-L
In C# 4, you'll have the Tuple type for your value, value pair.
在 C# 4 中,您将拥有值、值对的元组类型。
There's an MSDN articledescribing the type and the design decisions behind it.
有一篇MSDN 文章描述了它背后的类型和设计决策。