11

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.

11 Answers 11

31

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.

Sign up to request clarification or add additional context in comments.

7 Comments

how fetch back the values from the List of Tuple created ?
@HotTester : what do you mean by fetch back the values?
@HotTester: Use the properties of Tuple<,,> - but it'll be a lot less readable than creating your own class.
@HotTester: Yes, by creating your own type. If you are dead set against creating a new type, you could (ab)use cast by example
@HotTester: That's exactly the point of creating your own type (and potentially giving it behaviour, etc). You can't change the properties of Tuple, but you can create your own classes with your own properties...
|
14

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.

Comments

4

You can use a list of tuples. Like so:

List<Tuple<string, string, string>>

Comments

3

Maybe something like this:

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

Comments

3

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

Comments

2

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.

2 Comments

Thank link is very... Russian. Good thing i can read "Россия (Pусский)".
For those that can't read Russian... ;) msdn.microsoft.com/library/system.tuple.aspx
1

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.

Comments

1

Anyone tried dynamic ?

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

var name = list[0].Name;

Comments

0

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.

1 Comment

Actually scratch that, @Tigran's solution is not only better than mine, it taught me something I didn't know. Nice one.
0

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.

Comments

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

  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.