c# – 初始化KeyValuePair数组

栏目: C# · 发布时间: 7年前

内容简介:翻译自:https://stackoverflow.com/questions/4212657/initialising-a-keyvaluepair-array

这似乎是一件简单的事情,但我似乎无法找出正确的语法.我目前有这个:

KeyValuePair<string, string>[] kvpArr = new KeyValuePair<string,string>[];

但是,这似乎有效:

KeyValuePair<string, string>[] kvpArr = new KeyValuePair<string,string>[10];

但我最初不知道数组的大小.我知道我可以使用一系列KVP,我可能会这样做,但我只是想知道如何实现这一目标.

不,你不能这样做 – 因为数组总是有固定的大小.如果您没有指定该尺寸,您希望使用的尺寸是多少?您必须指定大小本身或内容(允许推断大小).例如:

int[] x = new int[10]; // Explicit size
int[] y = new int[] { 1, 2, 3, 4, 5 }; // Implicit size
int[] z = { 1, 2, 3, 4, 5 }; // Implicit size and type

列表与LT; T>绝对是您收藏的朋友,你不知道它的大小.

翻译自:https://stackoverflow.com/questions/4212657/initialising-a-keyvaluepair-array


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Cult of the Amateur

The Cult of the Amateur

Andrew Keen / Crown Business / 2007-6-5 / USD 22.95

Amateur hour has arrived, and the audience is running the show In a hard-hitting and provocative polemic, Silicon Valley insider and pundit Andrew Keen exposes the grave consequences of today’s......一起来看看 《The Cult of the Amateur》 这本书的介绍吧!

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具