HotTester
HotTester

Reputation: 5768

Make our own List<string, string, string>

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.

Upvotes: 11

Views: 68904

Answers (11)

Sharon
Sharon

Reputation: 767

Anyone tried dynamic ?

var list = new List<dynamic>();
list.Add(new { Name = "SampleN", Address = "SampleA", Email = "SampleE" });

var name = list[0].Name;

Upvotes: 1

Vivek
Vivek

Reputation: 39

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",
            });
        }
    }

Upvotes: 3

Vivek
Vivek

Reputation: 39

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");

  }
}

Upvotes: 0

Smit
Smit

Reputation: 609

For example:

var list = new List<Tuple<string, string, string>>();
var tuple = Tuple.Create("a", "b", "c");
list.Add(tuple);

For more information, look in MSDN.

Upvotes: 2

user900202
user900202

Reputation:

You may define a List and implement the needed interfaces(such as IList). Codes blow.

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.

Upvotes: 0

Tigran
Tigran

Reputation: 62248

You can use a Tuple to achieve that.

For example:

var list = new List<Tuple<string,string,string>>();

to iterate over the values you may want to do a simple:

    list.ForEach(x=>{
     //x is a Tuple
    });

or to find some specific tupple, you may want to 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.

Upvotes: 13

inksmithy
inksmithy

Reputation: 546

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.

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.

Upvotes: 0

Botz3000
Botz3000

Reputation: 39600

How about making a 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.

Upvotes: 1

Arion
Arion

Reputation: 31239

Maybe something like this:

var ls= new List<Tuple<string,string,string>>();

Upvotes: 3

Jon Skeet
Jon Skeet

Reputation: 1499770

You can certainly create your own class called List with three generic type parameters. I would strongly discourage you from doing so though. It would confuse the heck out of anyone using your code.

Instead, either use 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>.

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.

Upvotes: 31

user47589
user47589

Reputation:

You can use a list of tuples. Like so:

List<Tuple<string, string, string>>

Upvotes: 4

Related Questions