Advertisement
karlakmkj

LINQ - var

Nov 27th, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.74 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace LearnLinq
  6. {
  7.   class Program
  8.   {
  9.     static void Main()
  10.     {
  11.       List<string> heroes = new List<string> { "D. Va", "Lucio", "Mercy", "Soldier 76", "Pharah", "Reinhardt" };
  12.      
  13.       //var is an implicitly typed variable — we let the C# compiler determine the actual type for us.
  14.       var shortHeroes = from h in heroes
  15.           where h.Length < 8
  16.           select h;
  17.    
  18.       //print those of length shorter than 8 characters from the list
  19.       foreach (string hero in shortHeroes){
  20.         Console.WriteLine(hero);
  21.       }
  22.  
  23.       var longHeroes = heroes.Where(n => n.Length >8);
  24.  
  25.       Console.WriteLine(longHeroes.Count());
  26.     }
  27.   }
  28. }
  29.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement