Wednesday, July 16, 2008

List Comprehension in C# with LINQ - part 2

In this example, I tried to figure how to use
a static method to filter an array

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LINQArray
{
class Program
{
public static bool filter(string s)
{
bool ret = false;
if (s.StartsWith("A"))
{
ret = true;
}
return ret;
}

static void Main(string[] args)
{
string[] countries = { "Britain", "France", "Croatia", "Argentina", "Australia", "Mexico", "Finland",
"Spain", "Italy", "Greece" };
var thecountries = from c in countries
where filter(c)
select c;
foreach (var c in thecountries)
{
Console.WriteLine(c);
}
Console.ReadLine();
}
}
}

No comments:

Post a Comment