使用string和int数据类型在C#中创建列表

如何创建一个包含字符串值和int值的列表? 任何人都可以帮助我。是否可以创建具有不同数据类型的列表?

你可以使用:

var myList = new List>(); myList.Add(new KeyValuePair(1, "One"); foreach (var item in myList) { int i = item.Key; string s = item.Value; } 

或者如果您是.NET Framework 4,您可以使用:

 var myList = new List>(); myList.Add(Tuple.Create(1, "One")); foreach (var item in myList) { int i = item.Item1; string s = item.Item2; } 

如果字符串或整数对于集合是唯一的,您可以使用:

 Dictionary or Dictionary 

List是同质的。 唯一真正的方法是使用List来存储任何值。

您可以让列表包含您喜欢的任何对象。 为什么不创建自定义对象

自定义对象

 public class CustomObject { public string StringValue { get; set; } public int IntValue { get; set; } public CustomObject() { } public CustomObject(string stringValue, int intValue) { StringValue = stringValue; IntValue = intValue; } } 

列表创建

 List CustomObject = new List();