具有多个参数的C#列表 [英] C# List with more than one argument

查看:250
本文介绍了具有多个参数的C#列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某种原因,我今天什么都没想到,我已经进行了一段时间编程,应该知道答案.

出于某种原因,我记得使用List< string,string,string>

或使用多个参数的某种列表.

请帮助我.

For some reason I can''t think of anything today, I have been programming for a while and should know the answer to this.

For some reason I remember using a List<string,string,string>

Or a some kind of list that uses more than one argument.

Please help me.

推荐答案

你好

试试这个:
Hello

Try this:
public class MyClass
{
    public MyClass(params object[] objs)
    {
       foreach(object o in objs)
       {
            //Some UnBoxing Code...
       }
    }
}


并且:


And:

List<MyClass> myList = new List<MyClass>();

myList.Add(new MyClass(12,"",DateTime.Now));


列表在索引处只能包含一个对象(尽管该对象可以是列表,也可以是复杂的对象,如Mika的回答).我有一个建议是

A list can contain only one object at an index (though that object can be a list or a complex object, as in Mika''s answer). One suggestion I have is

List<Tuple<string,string,string>> Foo = new List<Tuple<string,string,string>>();
//Then you'd add:
Foo.Add(Tuple.Create"("Bar","Baz","Quux"));
//Or
Foo.Add(new Tuple<string,string,string>("Bar","Bax", "Quux");



显然,这不是Mika的建议可读性,而且它依赖于开发人员每次都正确获取字符串的顺序.但是,它确实更适合您的描述(而不是100%),所以也许这就是您所想的.



另一个建议(尽管它与您所说的不符)是"3d列表"(即,字符串列表的列表中的列表):



Obviously this is less readable them Mika''s suggestion, and it relies on the developer to get the order of the strings right each time. It does, however, fit your description better (but not 100%), so perhaps this is what you were thinking of.



One further suggestion (though it doesn''t really match what you say) is a "3d list" (i.e. a list of a list of a list of strings):

List<List<List<string>>>


但同样,第一个列表在每个索引处都包含一个项目,而恰好是另一个List.

最后,您看到的代码可能是一个通用类的实现,该类封装了您到目前为止所获得的各种答案中描述的类型处理.如果是这种情况,我将不得不质疑 MySpecialGenericList [0] 是什么意思,因为它从技术上存储了三个字符串,所有字符串都构成了该项目,但只有一个可以返回而不求助于元组或复杂对象.这些返回类型都不是传入的原始内容,因此在我看来,这在语义上是不正确的.


But again the first list contains a single item at each index, which just happens to be another List.

Finally, the code you saw could be an implementation of a generic class which wrappers up the type work up that are described in the various answers you''ve had so far. If this is the case, I''d have to question what MySpecialGenericList[0] would mean, as it tecnically stores three strings, all of which are required to form the item, but only one can be returned without resorting to a tuple or complex object. Either of these return types are not the original thing passed in, so this would, in my view, be symantically incorrect.


这取决于您的要求.如果您需要将多个字符串存储到列表中,则一种可能性是创建一个类来保存值并将该类的实例放置到列表中.像
It depends, what are you''re requirements. If you need to store several string to a list, one possibility is to create a class to hold the values and place instances of that class to the list. Something like
public class DataHolder {
   public string Text1 { get; set; }
   public string Text2 { get; set; }
   public string Text3 { get; set; }
}

...

public List<DataHolder> DataList = new public List<DataHolder>();

...

DataList.Add(new DataHolder() { Text1 = "A", Text2 = "B" };


如果您需要基于字典的列表,则可以使用 Dictionary< tkey,> [ ^ ]


If you need a dictionary based list then you can use Dictionary<tkey,>[^]


这篇关于具有多个参数的C#列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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