我试图将数组键设置为字符串,如下例所示,但在C#
.
<?php
$array = array();
$array['key_name'] = "value1";
?>
Run Code Online (Sandbox Code Playgroud)
Dan*_*Tao 56
你在C#中最接近的是Dictionary<TKey, TValue>
:
var dict = new Dictionary<string, string>();
dict["key_name"] = "value1";
Run Code Online (Sandbox Code Playgroud)
请注意,一个Dictionary<TKey, TValue>
是不一样的PHP的关联数组,因为它是唯一的访问由一种类型的键的(TKey
-这是string
在上述的例子),与串/整数密钥(感谢的帕维尔的组合用于澄清本点).
也就是说,我从未听说过.NET开发人员抱怨过这一点.
回应你的评论:
// The number of elements in headersSplit will be the number of ':' characters
// in line + 1.
string[] headersSplit = line.Split(':');
string hname = headersSplit[0];
// If you are getting an IndexOutOfRangeException here, it is because your
// headersSplit array has only one element. This tells me that line does not
// contain a ':' character.
string hvalue = headersSplit[1];
Run Code Online (Sandbox Code Playgroud)
你可以使用一个Dictionary<TKey, TValue>
:
Dictionary<string, string> dictionary = new Dictionary<string, string>();
dictionary["key_name"] = "value1";
Run Code Online (Sandbox Code Playgroud)
嗯我猜你想要一本字典:
using System.Collections.Generic;
// ...
var dict = new Dictionary<string, string>();
dict["key_name1"] = "value1";
dict["key_name2"] = "value2";
string aValue = dict["key_name1"];
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
59818 次 |
最近记录: |