Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- foreach (var c in collection)
- {
- DoSomething(c);
- }
- for (var i = 0; i < collection.Count; i++)
- {
- DoSomething(collection[i]);
- }
- select distinct [City] from [Person].[Address] order by [City]
- foreach (var city in Program.ListCities())
- {
- Console.Write(city + " ");
- if (city == "Boston")
- {
- break;
- }
- }
- var cities = Program.ListCities();
- for (var i = 0; i < cities.Count(); i++)
- {
- var city = cities.ElementAt(i);
- Console.Write(city + " ");
- if (city == "Boston")
- {
- break;
- }
- }
- var cities = Program.ListCities();
- var flushedCities = cities.ToList();
- for (var i = 0; i < flushedCities.Count; i++)
- {
- var city = flushedCities[i];
- Console.Write(city + " ");
- if (city == "Boston")
- {
- break;
- }
- }
- foreach (var city in Program.ListCities())
- {
- Console.Write(city + " ");
- if (city == "Boston")
- {
- break;
- }
- }
- using (var enumerator = Program.ListCities().GetEnumerator())
- {
- while (enumerator.MoveNext())
- {
- var city = enumerator.Current;
- Console.Write(city + " ");
- if (city == "Boston")
- {
- break;
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Data.SqlClient;
- using System.Diagnostics;
- using System.Linq;
- public class Program
- {
- private static int countCalls;
- private static int countYieldReturns;
- public static void Main()
- {
- Program.DisplayStatistics("for", Program.UseFor);
- Program.DisplayStatistics("for with list", Program.UseForWithList);
- Program.DisplayStatistics("while", Program.UseWhile);
- Program.DisplayStatistics("foreach", Program.UseForEach);
- Console.WriteLine("Press any key to continue...");
- Console.ReadKey(true);
- }
- private static void DisplayStatistics(string name, Action action)
- {
- Console.WriteLine("--- " + name + " ---");
- Program.countCalls = 0;
- Program.countYieldReturns = 0;
- var measureTime = Stopwatch.StartNew();
- action();
- measureTime.Stop();
- Console.WriteLine();
- Console.WriteLine();
- Console.WriteLine("The data was called {0} time(s) and yielded {1} item(s) in {2} ms.", Program.countCalls, Program.countYieldReturns, measureTime.ElapsedMilliseconds);
- Console.WriteLine();
- }
- private static void UseFor()
- {
- var cities = Program.ListCities();
- for (var i = 0; i < cities.Count(); i++)
- {
- var city = cities.ElementAt(i);
- Console.Write(city + " ");
- if (city == "Boston")
- {
- break;
- }
- }
- }
- private static void UseForWithList()
- {
- var cities = Program.ListCities();
- var flushedCities = cities.ToList();
- for (var i = 0; i < flushedCities.Count; i++)
- {
- var city = flushedCities[i];
- Console.Write(city + " ");
- if (city == "Boston")
- {
- break;
- }
- }
- }
- private static void UseForEach()
- {
- foreach (var city in Program.ListCities())
- {
- Console.Write(city + " ");
- if (city == "Boston")
- {
- break;
- }
- }
- }
- private static void UseWhile()
- {
- using (var enumerator = Program.ListCities().GetEnumerator())
- {
- while (enumerator.MoveNext())
- {
- var city = enumerator.Current;
- Console.Write(city + " ");
- if (city == "Boston")
- {
- break;
- }
- }
- }
- }
- private static IEnumerable<string> ListCities()
- {
- Program.countCalls++;
- using (var connection = new SqlConnection("Data Source=mframe;Initial Catalog=AdventureWorks;Integrated Security=True"))
- {
- connection.Open();
- using (var command = new SqlCommand("select distinct [City] from [Person].[Address] order by [City]", connection))
- {
- using (var reader = command.ExecuteReader(CommandBehavior.SingleResult))
- {
- while (reader.Read())
- {
- Program.countYieldReturns++;
- yield return reader["City"].ToString();
- }
- }
- }
- }
- }
- }
- public IList<Foo> GetListOfFoo()
- {
- var retVal=new List<Foo>();
- foreach(var foo in _myPrivateFooList)
- {
- if(foo.DistinguishingValue == check)
- {
- retVal.Add(foo);
- }
- }
- return retVal;
- }
- public IEnumerable<Foo> GetEnumerationOfFoo()
- {
- //no need to create an extra list
- //var retVal=new List<Foo>();
- foreach(var foo in _myPrivateFooList)
- {
- if(foo.DistinguishingValue == check)
- {
- //yield the match compiler handles the complexity
- yield return foo;
- }
- }
- //no need for returning a list
- //return retVal;
- }
- /**
- Returns an IEnumerable of fibonacci sequence
- **/
- public IEnumerable<int> Fibonacci()
- {
- int first, second = 1;
- yield return first;
- yield return second;
- //the 46th fibonacci number is the largest that
- //can be represented in 32 bits.
- for (int i = 3; i < 47; i++)
- {
- int retVal = first+second;
- first=second;
- second=retVal;
- yield return retVal;
- }
- }
- var thirtiethFib=Fibonacci().Skip(29).Take(1);
- var common = list1.Intersect(list2);
- List<int> common = new List<int>();
- for(int i1 = 0; i1 < list1.Count; i1++)
- {
- for(int i2 = 0; i2 < list2.Count; i2++)
- {
- if (list1[i1] == list2[i2])
- {
- common.Add(i1);
- break;
- }
- }
- }
- var foo = bar.Distinct();
Add Comment
Please, Sign In to add comment