Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make our own List<string, string, string>

Tags:

c#

.net

list

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.

like image 546
HotTester Avatar asked May 15 '12 09:05

HotTester


2 Answers

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.

like image 64
Jon Skeet Avatar answered Oct 10 '22 23:10

Jon Skeet


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.

like image 28
Tigran Avatar answered Oct 10 '22 22:10

Tigran