daily pastebin goal
18%
SHARE
TWEET

Untitled

a guest Feb 13th, 2018 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. foreach (var c in collection)
  2. {
  3.     DoSomething(c);
  4. }
  5.    
  6. for (var i = 0; i < collection.Count; i++)
  7. {
  8.     DoSomething(collection[i]);
  9. }
  10.    
  11. select distinct [City] from [Person].[Address] order by [City]
  12.    
  13. foreach (var city in Program.ListCities())
  14. {
  15.     Console.Write(city + " ");
  16.  
  17.     if (city == "Boston")
  18.     {
  19.         break;
  20.     }
  21. }
  22.    
  23. var cities = Program.ListCities();
  24. for (var i = 0; i < cities.Count(); i++)
  25. {
  26.     var city = cities.ElementAt(i);
  27.  
  28.     Console.Write(city + " ");
  29.  
  30.     if (city == "Boston")
  31.     {
  32.         break;
  33.     }
  34. }
  35.    
  36. var cities = Program.ListCities();
  37. var flushedCities = cities.ToList();
  38. for (var i = 0; i < flushedCities.Count; i++)
  39. {
  40.     var city = flushedCities[i];
  41.  
  42.     Console.Write(city + " ");
  43.  
  44.     if (city == "Boston")
  45.     {
  46.         break;
  47.     }
  48. }
  49.    
  50. foreach (var city in Program.ListCities())
  51. {
  52.     Console.Write(city + " ");
  53.  
  54.     if (city == "Boston")
  55.     {
  56.         break;
  57.     }
  58. }
  59.    
  60. using (var enumerator = Program.ListCities().GetEnumerator())
  61. {
  62.     while (enumerator.MoveNext())
  63.     {
  64.         var city = enumerator.Current;
  65.         Console.Write(city + " ");
  66.  
  67.         if (city == "Boston")
  68.         {
  69.             break;
  70.         }
  71.     }
  72. }
  73.    
  74. using System;
  75. using System.Collections.Generic;
  76. using System.Data;
  77. using System.Data.SqlClient;
  78. using System.Diagnostics;
  79. using System.Linq;
  80.  
  81. public class Program
  82. {
  83.     private static int countCalls;
  84.  
  85.     private static int countYieldReturns;
  86.  
  87.     public static void Main()
  88.     {
  89.         Program.DisplayStatistics("for", Program.UseFor);
  90.         Program.DisplayStatistics("for with list", Program.UseForWithList);
  91.         Program.DisplayStatistics("while", Program.UseWhile);
  92.         Program.DisplayStatistics("foreach", Program.UseForEach);
  93.  
  94.         Console.WriteLine("Press any key to continue...");
  95.         Console.ReadKey(true);
  96.     }
  97.  
  98.     private static void DisplayStatistics(string name, Action action)
  99.     {
  100.         Console.WriteLine("--- " + name + " ---");
  101.  
  102.         Program.countCalls = 0;
  103.         Program.countYieldReturns = 0;
  104.  
  105.         var measureTime = Stopwatch.StartNew();
  106.         action();
  107.         measureTime.Stop();
  108.  
  109.         Console.WriteLine();
  110.         Console.WriteLine();
  111.         Console.WriteLine("The data was called {0} time(s) and yielded {1} item(s) in {2} ms.", Program.countCalls, Program.countYieldReturns, measureTime.ElapsedMilliseconds);
  112.         Console.WriteLine();
  113.     }
  114.  
  115.     private static void UseFor()
  116.     {
  117.         var cities = Program.ListCities();
  118.         for (var i = 0; i < cities.Count(); i++)
  119.         {
  120.             var city = cities.ElementAt(i);
  121.  
  122.             Console.Write(city + " ");
  123.  
  124.             if (city == "Boston")
  125.             {
  126.                 break;
  127.             }
  128.         }
  129.     }
  130.  
  131.     private static void UseForWithList()
  132.     {
  133.         var cities = Program.ListCities();
  134.         var flushedCities = cities.ToList();
  135.         for (var i = 0; i < flushedCities.Count; i++)
  136.         {
  137.             var city = flushedCities[i];
  138.  
  139.             Console.Write(city + " ");
  140.  
  141.             if (city == "Boston")
  142.             {
  143.                 break;
  144.             }
  145.         }
  146.     }
  147.  
  148.     private static void UseForEach()
  149.     {
  150.         foreach (var city in Program.ListCities())
  151.         {
  152.             Console.Write(city + " ");
  153.  
  154.             if (city == "Boston")
  155.             {
  156.                 break;
  157.             }
  158.         }
  159.     }
  160.  
  161.     private static void UseWhile()
  162.     {
  163.         using (var enumerator = Program.ListCities().GetEnumerator())
  164.         {
  165.             while (enumerator.MoveNext())
  166.             {
  167.                 var city = enumerator.Current;
  168.                 Console.Write(city + " ");
  169.  
  170.                 if (city == "Boston")
  171.                 {
  172.                     break;
  173.                 }
  174.             }
  175.         }
  176.     }
  177.  
  178.     private static IEnumerable<string> ListCities()
  179.     {
  180.         Program.countCalls++;
  181.         using (var connection = new SqlConnection("Data Source=mframe;Initial Catalog=AdventureWorks;Integrated Security=True"))
  182.         {
  183.             connection.Open();
  184.  
  185.             using (var command = new SqlCommand("select distinct [City] from [Person].[Address] order by [City]", connection))
  186.             {
  187.                 using (var reader = command.ExecuteReader(CommandBehavior.SingleResult))
  188.                 {
  189.                     while (reader.Read())
  190.                     {
  191.                         Program.countYieldReturns++;
  192.                         yield return reader["City"].ToString();
  193.                     }
  194.                 }
  195.             }
  196.         }
  197.     }
  198. }
  199.    
  200. public IList<Foo> GetListOfFoo()
  201. {
  202.    var retVal=new List<Foo>();
  203.    foreach(var foo in _myPrivateFooList)
  204.    {
  205.       if(foo.DistinguishingValue == check)
  206.       {
  207.          retVal.Add(foo);
  208.       }
  209.    }
  210.    return retVal;
  211. }
  212.    
  213. public IEnumerable<Foo> GetEnumerationOfFoo()
  214. {
  215.    //no need to create an extra list
  216.    //var retVal=new List<Foo>();
  217.    foreach(var foo in _myPrivateFooList)
  218.    {
  219.       if(foo.DistinguishingValue == check)
  220.       {
  221.          //yield the match compiler handles the complexity
  222.          yield return foo;
  223.       }
  224.    }
  225.    //no need for returning a list
  226.    //return retVal;
  227. }
  228.    
  229. /**
  230. Returns an IEnumerable of fibonacci sequence
  231. **/
  232. public IEnumerable<int> Fibonacci()
  233. {
  234.   int first, second = 1;
  235.   yield return first;
  236.   yield return second;
  237.   //the 46th fibonacci number is the largest that
  238.   //can be represented in 32 bits.
  239.   for (int i = 3; i < 47; i++)
  240.   {
  241.     int retVal = first+second;
  242.     first=second;
  243.     second=retVal;
  244.     yield return retVal;
  245.   }
  246. }
  247.    
  248. var thirtiethFib=Fibonacci().Skip(29).Take(1);
  249.    
  250. var common = list1.Intersect(list2);
  251.    
  252. List<int> common = new List<int>();
  253. for(int i1 = 0; i1 < list1.Count; i1++)
  254. {
  255.     for(int i2 = 0; i2 < list2.Count; i2++)
  256.     {
  257.         if (list1[i1] == list2[i2])
  258.         {
  259.             common.Add(i1);
  260.             break;
  261.         }
  262.     }
  263. }
  264.    
  265. var foo = bar.Distinct();
RAW Paste Data
Top