C# 制作我们自己的 List<string, string, string>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10597629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Make our own List<string, string, string>
提问by HotTester
Can we make our own List<string, string, string>in C#.NET? I need to make a list having 3 different strings as a single element in the list.
我们可以List<string, string, string>在 C#.NET 中创建我们自己的吗?我需要制作一个包含 3 个不同字符串的列表作为列表中的单个元素。
采纳答案by Jon Skeet
You can certainly create your own class called Listwith three generic type parameters. I would stronglydiscourage you from doing so though. It would confuse the heck out of anyone using your code.
您当然可以创建自己的类,List使用三个泛型类型参数调用。不过,我强烈建议您不要这样做。它会使使用您的代码的任何人感到困惑。
Instead, eitheruse List<Tuple<string, string, string>>(if you're using .NET 4, anyway) or (preferrably) create your own class to encapsulate those three strings in a meaningful way, then use List<YourNewClass>.
相反,要么使用List<Tuple<string, string, string>>(如果您使用 .NET 4,无论如何)或(最好)创建您自己的类以有意义的方式封装这三个字符串,然后使用List<YourNewClass>.
Creating your own class will make it much clearer when you're writing and reading the code - by giving names to the three different strings involved, everyone will know what they're meant to mean. You can also give more behaviour to the class as and when you need to.
创建您自己的类将使您在编写和阅读代码时更加清晰 - 通过为所涉及的三个不同字符串命名,每个人都会知道它们的含义。您还可以在需要时为类提供更多行为。
回答by Arion
Maybe something like this:
也许是这样的:
var ls= new List<Tuple<string,string,string>>();
回答by Botz3000
How about making a List<Tuple<string, string, string>>?
做一个List<Tuple<string, string, string>>怎么样?
A better idea might be though, if each of the strings has a specific meaning, to put them all into a class and then create a List of that.
不过,更好的想法可能是,如果每个字符串都有特定的含义,将它们全部放入一个类中,然后创建一个列表。
回答by Tigran
You can use a Tupleto achieve that.
您可以使用元组来实现这一点。
For example:
例如:
var list = new List<Tuple<string,string,string>>();
to iterate over the values you may wantto do a simple:
迭代你可能想要做的一个简单的值:
list.ForEach(x=>{
//x is a Tuple
});
or to findsome specific tupple, you may wantto do the folowing:
或者要找到一些特定的元组,您可能需要执行以下操作:
var list = new List<Tuple<string,string,string>>{
new Tuple<string,string,string>("Hello", "Holla", "Ciao"),
new Tuple<string,string,string>("Buy", "--", "Ciao")
};
list.Where(x=>x.Item2 == "--");
This will return the last Tuple.
这将返回最后一个元组。
回答by inksmithy
No, sadly this does't work. The best you can do is to create a List of Lists, or utilise some form of implementation of the IDictionary interface.
不,遗憾的是这不起作用。您能做的最好的事情是创建一个列表列表,或者利用 IDictionary 接口的某种形式的实现。
Although, that said, if you have three items of data which belong together in this manner, it's probably worthwhile creating a class to contain them.
尽管如此,如果您有三项以这种方式属于一起的数据,那么创建一个类来包含它们可能是值得的。
That would then give you the opportunity to pass a List around your application and assign meaningful names to each data item. Never underestimate the power of readability six months down the line.
这将使您有机会在您的应用程序周围传递一个 List 并为每个数据项分配有意义的名称。永远不要低估六个月后可读性的力量。
回答by Allen Xia
You may define a List and implement the needed interfaces(such as IList). Codes blow.
您可以定义一个 List 并实现所需的接口(例如 IList)。代码吹。
public class List<T1, T2, T3> : IList
{
#region IList Members
public int Add(object value)
{
throw new NotImplementedException();
}
public void Clear()
{
throw new NotImplementedException();
}
public bool Contains(object value)
{
throw new NotImplementedException();
}
public int IndexOf(object value)
{
throw new NotImplementedException();
}
public void Insert(int index, object value)
{
throw new NotImplementedException();
}
public bool IsFixedSize
{
get { throw new NotImplementedException(); }
}
public bool IsReadOnly
{
get { throw new NotImplementedException(); }
}
public void Remove(object value)
{
throw new NotImplementedException();
}
public void RemoveAt(int index)
{
throw new NotImplementedException();
}
public object this[int index]
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
#endregion
#region ICollection Members
public void CopyTo(Array array, int index)
{
throw new NotImplementedException();
}
public int Count
{
get { throw new NotImplementedException(); }
}
public bool IsSynchronized
{
get { throw new NotImplementedException(); }
}
public object SyncRoot
{
get { throw new NotImplementedException(); }
}
#endregion
#region IEnumerable Members
public IEnumerator GetEnumerator()
{
throw new NotImplementedException();
}
#endregion
}
However, List<Tuple<string,string,string>>is recommended.
但是,List<Tuple<string,string,string>>建议。
回答by Vivek
public class Listt< T, T1, T2>
{
public Listt()
{
}
public void Add(T item, T1 item1, T2 item3)
{
}
}
public partial class MyApp : Window
{
public MyApp()
{
InitializeComponent();
Listt<string, string, string> customList = new Listt<string, string, string>();
customList.Add("we","are","here");
}
}
回答by Smit
回答by Vivek
I recommend this solution
我推荐这个解决方案
public class MyCustomClass
{
public string MyString1 { get; set; }
public string MyString2 { get; set; }
public string MyString3 { get; set; }
}
class MyApp
{
public MyApp()
{
List<MyCustomClass> customList = new List<MyCustomClass>();
customList.Add(new MyCustomClass
{
MyString1 = "Hello",
MyString2 = "Every",
MyString3 = "Body",
});
}
}

