C# 制作我们自己的列表<;字符串,字符串,字符串>;
我们可以在C#NET中创建自己的C# 制作我们自己的列表<;字符串,字符串,字符串>;,c#,.net,list,C#,.net,List,我们可以在C#NET中创建自己的列表吗?我需要制作一个包含3个不同字符串的列表,作为列表中的单个元素。您可以使用。像这样: List<Tuple<string, string, string>> 列表 您当然可以创建自己的类,名为List,包含三个泛型类型参数。不过,我强烈劝阻你不要这样做。这会让任何使用你的代码的人感到困惑 相反,可以使用List(如果您使用的是.NET4),或者(最好)创建自己的类以有意义的方式封装这三个字符串,然后使用List 创建自己的类将使您在
列表吗?我需要制作一个包含3个不同字符串的列表,作为列表中的单个元素。您可以使用。像这样:
List<Tuple<string, string, string>>
列表
您当然可以创建自己的类,名为List
,包含三个泛型类型参数。不过,我强烈劝阻你不要这样做。这会让任何使用你的代码的人感到困惑
相反,可以使用List
(如果您使用的是.NET4),或者(最好)创建自己的类以有意义的方式封装这三个字符串,然后使用List
创建自己的类将使您在编写和阅读代码时更加清晰-通过为所涉及的三个不同字符串命名,每个人都将知道它们的含义。您还可以在需要时为该类提供更多的行为。可能是这样的:
var ls= new List<Tuple<string,string,string>>();
var ls=new List();
制作一个列表怎么样
但是,如果每个字符串都有特定的含义,则更好的方法是将它们全部放入一个类中,然后创建一个该类的列表。您可以使用A来实现这一点
例如:
var list = new List<Tuple<string,string,string>>();
或者,要查找某个特定的元组,您可能需要执行以下操作:
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 == "--");
var list=新列表{
新元组(“你好”、“你好”、“再见”),
新元组(“买”、“买”、“--”、“Ciao”)
};
列表。其中(x=>x.Item2==“-”;
这将返回最后一个元组 不,遗憾的是这不起作用。您最好创建一个列表列表,或者使用IDictionary接口的某种形式的实现
尽管如此,如果您有三个数据项以这种方式属于一起,那么创建一个类来包含它们可能是值得的
这样,您就有机会在应用程序周围传递一个列表,并为每个数据项指定有意义的名称。永远不要低估六个月后可读性的力量 您可以定义一个列表并实现所需的接口(如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
}
公共类列表:IList
{
#国际劳工组织成员区域
公共整数加法(对象值)
{
抛出新的NotImplementedException();
}
公共空间清除()
{
抛出新的NotImplementedException();
}
公共布尔包含(对象值)
{
抛出新的NotImplementedException();
}
public int IndexOf(对象值)
{
抛出新的NotImplementedException();
}
公共void插入(int索引,对象值)
{
抛出新的NotImplementedException();
}
公共图书馆是固定大小的
{
获取{抛出新的NotImplementedException();}
}
公共图书馆是只读的
{
获取{抛出新的NotImplementedException();}
}
公共无效删除(对象值)
{
抛出新的NotImplementedException();
}
公共无效删除(整数索引)
{
抛出新的NotImplementedException();
}
公共对象此[int索引]
{
得到
{
抛出新的NotImplementedException();
}
设置
{
抛出新的NotImplementedException();
}
}
#端区
#区域i集合成员
public void CopyTo(数组,int索引)
{
抛出新的NotImplementedException();
}
公共整数计数
{
获取{抛出新的NotImplementedException();}
}
公共布尔值是同步的
{
获取{抛出新的NotImplementedException();}
}
公共对象同步根
{
获取{抛出新的NotImplementedException();}
}
#端区
#区域可数成员
公共IEnumerator GetEnumerator()
{
抛出新的NotImplementedException();
}
#端区
}
但是,建议使用列表。公共类列表T
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");
}
}
{
公共列表()
{
}
公共作废添加(T项、T1项1、T2项3)
{
}
}
公共部分类MyApp:Window
{
公共MyApp()
{
初始化组件();
Listt customList=new Listt();
添加(“我们”、“是”、“在这里”);
}
}
例如:
var list = new List<Tuple<string, string, string>>();
var tuple = Tuple.Create("a", "b", "c");
list.Add(tuple);
var list=newlist();
var tuple=tuple.Create(“a”、“b”、“c”);
添加(元组);
有关更多信息,请查看。我推荐此解决方案
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",
});
}
}
公共类MyCustomClass
{
公共字符串MyString1{get;set;}
公共字符串MyString2{get;set;}
公共字符串MyString3{get;set;}
}
类MyApp
{
公共MyApp()
{
List customList=新列表();
添加(新的MyCustomClass)
{
MyString1=“你好”,
MyString2=“每个”,
MyString3=“Body”,
});
}
}
有人尝试过动态吗
var list = new List<dynamic>();
list.Add(new { Name = "SampleN", Address = "SampleA", Email = "SampleE" });
var name = list[0].Name;
var list=newlist();
添加(新的{Name=“SampleN”,Address=“samreake”,Email=“SampleE”});
变量名称=列表[0]。名称;
如何从创建的元组列表中取回值?@HotTester:取回值是什么意思?@HotTester:使用元组的属性
-但是它比创建自己的类可读性差得多。@HotTester:是的,通过创建自己的类型。如果你坚决反对创建一个新类型,你可以@HotTester:这正是创建你自己的类型(并可能赋予它行为等)的要点。你不能改变元组的属性,但你可以用你自己的属性创建你自己的类……事实上,@Tigran的解决方案不仅比我的好,还教会了我一些我不知道的东西。很好的一个。谢谢链接是非常。。。俄语幸好我能读“俄语”,对于那些不懂俄语的人…;)
var list = new List<dynamic>();
list.Add(new { Name = "SampleN", Address = "SampleA", Email = "SampleE" });
var name = list[0].Name;