在C#中处理字符串创建列表和int数据类型 [英] Create list in C# with string and int datatypes

查看:170
本文介绍了在C#中处理字符串创建列表和int数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何可以创建一个同时拥有字符串值,int值的列表? ?
谁能帮我please.Is可以创建具有不同数据类型的列表

How can I create a list which holds both string value and int value? Can anyone help me please.Is it possible to create a list with different datatypes?

推荐答案

您可以使用:

var myList = new List<KeyValuePair<int, string>>();
myList.Add(new KeyValuePair<int, string>(1, "One");

foreach (var item in myList)
{
    int i = item.Key;
    string s = item.Value;
}

或如果你是.NET Framework 4中,你可以使用:

or if you are .NET Framework 4 you could use:

var myList = new List<Tuple<int, string>>();
myList.Add(Tuple.Create(1, "One"));

foreach (var item in myList)
{
    int i = item.Item1;
    string s = item.Item2;
}

如果任字符串或整数将是唯一的一套,你可以使用:

If either the string or integer is going to be unique to the set, you could use:

Dictionary<int, string> or Dictionary<string, int>

这篇关于在C#中处理字符串创建列表和int数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆