Create your own list <string, string, string>
Can we create our own List<string, string, string> in C # .NET? I need to create a list of 3 different lines as a single item in a list.
You can create your own class called List with three generic type parameters. I would strongly discourage you from this. This would confuse everyone who used your code.
Instead, use List<Tuple<string, string, string>> (if you are still using .NET 4) or (preferably) create your own class to encapsulate these three lines in a meaningful way, then use List<YourNewClass> .
Creating your own class will make it more understandable when you write and read code - by specifying names on three different lines, everyone will know what they mean. You can also give more behavior to the class as needed.
You can use Tuple to achieve this.
For example:
var list = new List<Tuple<string,string,string>>(); to iterate over values you can make simple:
list.ForEach(x=>{ //x is a Tuple }); or find any specific example, you can do the following:
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.
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", }); } } Maybe something like this:
var ls= new List<Tuple<string,string,string>>(); You can use the list of tuples . For example:
List<Tuple<string, string, string>> For example:
var list = new List<Tuple<string, string, string>>(); var tuple = Tuple.Create("a", "b", "c"); list.Add(tuple); See MSDN for more information.
How to make List<Tuple<string, string, string>> ?
A better idea might be, though, if each of the lines has a specific meaning, to put them all in a class, and then create a list of that.
No, unfortunately, this does not work. The best you can do is create a list of lists or use some form of implementation of the IDictionary interface.
Even though if you have three data items that belong together in this way, it's probably worth creating a class to contain them.
This will give you the ability to pass a list around your application and assign meaningful names to each data item. Never underestimate the power of readability after six months.
You can define a list and implement the necessary interfaces (e.g. IList). Codes are blowing.
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>> recommended.
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"); } } Has anyone tried dynamic ?
var list = new List<dynamic>(); list.Add(new { Name = "SampleN", Address = "SampleA", Email = "SampleE" }); var name = list[0].Name;