你能用C#做$array[“string”]吗


Can you do $array["string"] in C#

可能重复:
将数组键设置为字符串而不是int?

我知道你可以做这样的事情php中的$array["string"],但您是否也可以在C#中这样做,而不是使用带数字的数组?

PHP中的数组实际上更像C#中的字典。因此,是的,您可以使用Dictionary<string, YourValueType>:来执行此操作
var dict = new Dictionary<string, int>();
dict["hello"] = 42;
dict["world"] = 12;

如果您想存储键/值对,您可能会使用Dictionary:http://msdn.microsoft.com/en-us/library/xfhwa508.aspx

在PHP中,"数组"类实际上是一个映射,它是一个将值映射到键的数据结构。

在C#中有很多不同的数据结构类。每个人都有自己的特点
维基百科是阅读基本数据结构的一个很好的起点:http://en.wikipedia.org/wiki/Data_structure#Common_data_structures

为此使用Dictionary:(

var dictionary = new Dictionary<string, string>();
dictionary["key"] = "value";

依此类推

var ar = new string[]{"one", "two"};