I have got a question. See the two blocks of code. Isn't Where(), OrderBy() and Select() from IEnumerable Class supposed to take a delegate type, lambda expression or anonymous type as a parameter. If so, how did the QueryOverStringWithRawDelegate() produce the same results as QueryOverStringsWithExtensionMethods()?
void QueryOverStringsWithExtensionMethods()
{
// Assume we have an array of strings
string[] currentVideoGames = { "Morrowind", "Uncharted 2", "Fallout 3", "Daxter", "Bio Shock 4" };
IEnumerable<string> subset = currentVideoGames.Where(game => game.Contains(" ")).OrderBy(game => game).Select(delegate (string game) { return game; });
Console.WriteLine("Query Over Strings With Extension Method");
foreach (var s in subset)
{
Console.WriteLine("Items: {0}", s);
}
}
and
void QueryStringsWithRawDelegates()
{
// Assume we have an array of strings
string[] currentVideoGames = { "Morrowind", "Uncharted 2", "Fallout 3", "Daxter", "Bio Shock 4" };
var subset = currentVideoGames.Where(Filter).OrderBy(ProcessItems).Select(ProcessItems);
foreach (var s in subset)
{
Console.WriteLine("Items: {0}", s);
}
string ProcessItems(string game)
{
return game;
}
bool Filter(string game)
{
return game.Contains(" ");
}
}
Thank you for your help !