LINQ's new Zip operator

Ambily.raj
Posted by Ambily.raj under LINQ category on | Views : 2949
Zip operator is introduced as part of .Net framework 4.0. Zip operator merge two collections into one.
In the following example, it marges the names collection with corresponding ages.
static void Main(string[] args)
{
string[] names = { "Anju", "Srinivas" };
int[] ages = { 25, 60 };

IEnumerable<string> result=
names.Zip(ages, (name, age) => name + " is " + age + " years old.");

foreach (string st in result)
{
Console.WriteLine(st);
}

Console.Read();
}


Result

Anju is 25 years old.
Srinivas is 60 years old.

We can give varying size collections as input like
string[] names = { "Manu" };
int[] ages = { 25, 60 };
IEnumerable<string> result=names.Zip(ages, (name, age) => name + " is " + age + " years old.");


Result
Manu is 25 years old.

Comments or Responses

Login to post response