I am trying to set the array keys as a strings like in the example below, but inC#.
我试图将数组键设置为字符串,如下面的示例所示,但是是inC#。
6 个解决方案
#1
52
The closest you get in C# is Dictionary:
最接近c#的是Dictionary
:
var dict = new Dictionary();
dict["key_name"] = "value1";
Note that a Dictionary is not the same as PHP's associative array, because it is only accessible by one type of key (TKey -- which is string in the above example), as opposed to a combination of string/integer keys (thanks to Pavel for clarifying this point).
That said, I've never heard a .NET developer complain about that.
也就是说,我从来没有听到。net开发人员对此有过抱怨。
In response to your comment:
回应你的评论:
// 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];
#2
5
Uhm I'm guessing you want a dictionary:
嗯,我猜你想要一本字典:
using System.Collections.Generic;
// ...
var dict = new Dictionary();
dict["key_name1"] = "value1";
dict["key_name2"] = "value2";
string aValue = dict["key_name1"];
#3
4
You could use a Dictionary:
可以使用字典
:
Dictionary dictiOnary= new Dictionary();
dictionary["key_name"] = "value1";
#4
2
Try a dictionary:
试试一个字典:
var dictiOnary= new Dictionary();
dictionary.Add("key_name", "value1");
#5
0
You can also use a KeyedCollection http://msdn.microsoft.com/en-us/library/ms132438%28v=vs.110%29.aspx where your value is a complex type and has a unique property.
Since everyone else said dictionary, I decided I would answer with 2 arrays. One array would be an index into the other.
既然大家都说字典,我决定用两个数组来回答。一个数组是另一个数组的索引。
You didn't really specify the data type that you would find at a particular index so I went ahead and chose string for my example.
您并没有指定在特定索引处可以找到的数据类型,因此我选择了string作为示例。
You also didn't specify if you wanted to be able to resize this later. If you do, you would use List instead of T [] where T is the type and then just expose some public methods for add for each list if desired.
Here is how you could do it. This could also be modified to pass in the possible indexes to the constructor or make it however you would do it.
这是你如何做的。还可以修改它,将可能的索引传递给构造函数,或者让它按照您希望的方式进行。
class StringIndexable
{
//you could also have a constructor pass this in if you want.
public readonly string[] possibleIndexes = { "index1", "index2","index3" };
private string[] rowValues;
public StringIndexable()
{
rowValues = new string[ColumnTitles.Length];
}
///
/// Will Throw an IndexOutofRange Exception if you mispell one of the above column titles
///
///
///
public string this [string index]
{
get { return getOurItem(index); }
set { setOurItem(index, value); }
}
private string getOurItem(string index)
{
return rowValues[possibleIndexes.ToList().IndexOf(index.ToLower())];
}
private void setOurItem(string index, string value)
{
rowValues[possibleIndexes.ToList().IndexOf(index.ToLower())] = value;
}
}