以文本方式定义数组和键-C#

以文本方式定义数组和键-C#,c#,arrays,key-value,C#,Arrays,Key Value,试图巩固这个 string[] array = new string[]; array[0] = "Index 0"; array[3] = "Index 3"; array[4] = "index 4"; 排成一行 PHP中的示例 $array = array( 0 => "Index 0", 3 => "Index 3", 4 => "Index 4" ); 我知道我能做到 string[] array = { "string1", "string2", "strin

试图巩固这个

string[] array = new string[];

array[0] = "Index 0";
array[3] = "Index 3";
array[4] = "index 4";
排成一行

PHP中的示例

$array = array( 0 => "Index 0", 3 => "Index 3", 4 => "Index 4" );
我知道我能做到

string[] array = { "string1", "string2", "string3" }

但是,我如何才能在其中获得适当的索引呢?

听起来您真正想要的是一本
字典,而不是传统的C#数组:

var字典=新字典
{
{0,“索引0”},
{3,“索引3”},
{4,“索引4”}
};
在C语言中,你不能。如果需要特定索引,则必须传入
null
值以保留空对象的位置


听起来你真的在找一本
字典

据我所知,你不能跳过规则数组中的索引号(例如,0,1,2,然后4没有3)。您需要使用不同的数据结构,如字典或哈希表

Hashtable ht = new Hashtable()
{
    {"key1", "value1"},
    {"key2", "value2"}
};

据我所知,您无法按所需的方式定义数组。其他海报表明您可以使用关联数组(字典)

您最多可以创建一个变通方法:


字符串[]数组={“array0”、“”、“array2”、“array3”};


字符串[]数组=新字符串[4];
数组[0]=“数组0”;
数组[2]=“array2”;

Hashtable ht = new Hashtable()
{
    {"key1", "value1"},
    {"key2", "value2"}
};