Could one be preferred over the other in terms of performance? Both option have to traverse the whole array somehow to find out which string contains an 'a'. Or does it not matter much and is it just a syntax thing.
Or are there better ways to do this?
All your opinions are greatly appriciated. Here's the code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace ConsoleApplication10
{
class Program
{
static void Main(string[] args)
{
string[] fruits = { "prune", "apple", "pear", "banana", "cherry", "orange", "blueberry" };
Console.WriteLine(" --- Option1 use of LINQ");
Option1(fruits);
Console.WriteLine(" --- Option2 use of List");
Option2(fruits);
Console.ReadKey();
}
static void Option1(string[] F)
{
IEnumerable<string> query = F.Where(fruit => fruit.Contains("a"));
foreach (string f in query)
{
Console.WriteLine(f);
}
}
static void Option2(string[] F)
{
List<string> query = new List<string>();
for (int i = 0; i < F.Count(); i++)
{
if (F[i].Contains("a"))
{
query.Add(F[i]);
}
}
foreach (string f in query)
{
Console.WriteLine(f);
}
}}