C#List或带字符串和数字的数组

Mar*_*o M -2 c# arrays arraylist multidimensional-array c#-4.0

创建包含字符串和数字的数组或列表的最佳方法是什么?

使用Dictionary,可以这样做:

Dictionary < string, int > dict = new Dictionary < string, int > ();
Run Code Online (Sandbox Code Playgroud)

但是有更好的方法使用Array或List吗?

我需要它来存储IP地址和相应的端口号.

rec*_*ive 5

最好的方法取决于您的用例.但这是一个简单的方法.

var list = new List<Tuple<string, int>> {
    Tuple.Create("foo", 123),
    Tuple.Create("bar", 234),
};
Run Code Online (Sandbox Code Playgroud)

如果你想要一个相关的IP地址和端口列表,我会为它制作一个专用的类型.

struct Address {
    public string Ip;
    public int Port;
}

...

var list = new List<Address>();
Run Code Online (Sandbox Code Playgroud)