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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With