300x250 AD TOP

Search This Blog

Paling Dilihat

Powered by Blogger.

Friday, June 1, 2012

Tuples

So what are tuples?


Tuples are a generic data structures, it can contain 1 - 8 data items.


What are they used for? anything you want them to, they are serializable, so you can use them in your WCF projects, they are simple, so you can use them when you want to pass a number of parameters back from a method without creating a special structure just for that method.


Lets say I have a method which returns an ordered and paged list of items, but I also want to know how many items are in the list without going to a different method.


You can do something like this:


public static Tuple<int, List<string>> GetPagedItems(int from, int len)
        {
            var list = GenerateStringList(1000);

            var listcount  = list.Count();
            var subset = list.OrderBy(i => i).Skip(from).Take(len).ToList();

            return new Tuple<int, List<string>>(listcount, subset);
        }


The uses are only limited by your imagination, since .NET supports covariance and contravariance since 4.0, the sky is the limit.

Tags:

0 comments:

Post a Comment