C# 如何以正确的方式初始化 KeyValuePair 对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15495165/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 16:59:42  来源:igfitidea点击:

How to initialize KeyValuePair object the proper way?

c#initialization

提问by

I've seen in (amongst others) this questionthat people wonder how to initialize an instance of KeyValuePair, which expectedly should look like this.

我在(除其他外)这个问题中看到,人们想知道如何初始化KeyValuePair的实例,它应该看起来像这样。

KeyValuePair<int, int> keyValuePair = new KeyValuePair<int, int>
{ 
  Key = 1,
  Value = 2
};

It doesn't work, as if the properties aren't there. Intead, I need to use the constructor like this.

它不起作用,好像属性不存在一样。Intead,我需要像这样使用构造函数。

KeyValuePair<int, int> keyValuePair = new KeyValuePair<int, int>(1, 2);

Admittedly shorter syntax but it bothers me that I can't use the initializer. What am I doing wrong?

无可否认,语法较短,但我无法使用初始化程序让我感到困扰。我究竟做错了什么?

回答by Phil

The Keyand Valueproperties are read-only, therefore you can't use them in an object initializer.

关键价值属性是只读的,因此,你不能在一个对象初始化使用它们。

See this entry in the C# programming guide.

请参阅C# 编程指南中的此条目。

回答by Dennis

KeyValuePair<int, int>is a struct, and, fortunately, it is immutablestruct. In particular, this means that its properties are read only. So, you can't use object intializer for them.

KeyValuePair<int, int>是一个结构体,幸运的是,它是不可变的结构体。特别是,这意味着它的属性是只读的。因此,您不能为它们使用对象初始化器。

回答by nvoigt

You aren't doing something wrong. The KeyValuePairs properties are read-only. You cannot set them. Additionally, there is no empty default constructor. You need to use the constructor provided.

你没有做错什么。KeyValuePairs 属性是只读的。你不能设置它们。此外,没有空的默认构造函数。您需要使用提供的构造函数。

回答by Kevin Holditch

You are not wrong you have to initialise a keyValuePair using

你没有错,你必须使用初始化一个 keyValuePair

KeyValuePair<int, int> keyValuePair = new KeyValuePair<int, int>(1, 2);

The reason that you cannot use the object initialisation syntax ie { Key = 1, Value = 2 } is because the Key and Value properties have no setters only getters (they are readonly). So you cannot even do:

不能使用对象初始化语法即 { Key = 1, Value = 2 } 的原因是因为 Key 和 Value 属性没有设置器只有获取器(它们是只读的)。所以你甚至不能这样做:

keyValuePair.Value = 1; // not allowed

回答by Mihail Shishkov

The Key and Value properties have no setters. Thats why you can't use them in the initializer. Just use the constructor :) and you'll be fine.

Key 和 Value 属性没有设置器。这就是为什么您不能在初始化程序中使用它们的原因。只需使用构造函数 :) 就可以了。

回答by nawfal

Ok you have the answers. As an alternative, I prefer factory pattern similar to Tupleclass, for type inference magic :)

好的,你有答案了。作为替代方案,我更喜欢类似于Tuple类的工厂模式,用于类型推断魔术:)

public static class KeyValuePair
{
    public static KeyValuePair<K, V> Create<K, V>(K key, V value)
    {
        return new KeyValuePair<K, V>(key, value);
    }
}

So short becomes shorter:

所以短变得更短:

var keyValuePair = KeyValuePair.Create(1, 2);

回答by Paulo Carvalho

Here goes an example that does the job

这是一个可以完成工作的示例

KeyValuePair<int, int> kvp = new KeyValuePair<int, int>(1, 1);

回答by flodis

Dictionaries have compact initializers:

字典具有紧凑的初始值设定项:

var imgFormats = new Dictionary<string, ChartImageFormat>()
{
    {".bmp", ChartImageFormat.Bmp}, 
    {".gif", ChartImageFormat.Gif}, 
    {".jpg", ChartImageFormat.Jpeg}, 
    {".jpeg", ChartImageFormat.Jpeg}, 
    {".png", ChartImageFormat.Png}, 
    {".tiff", ChartImageFormat.Tiff}, 
};

In this case the dictionary i used to associate file extensions with image format constants of chart objects.

在这种情况下,我使用字典将文件扩展名与图表对象的图像格式常量相关联。

A single keyvaluepair can be returned from the dictionary like this:

可以像这样从字典中返回单个键值对:

var pair = imgFormats.First(p => p.Key == ".jpg");