Created
March 2, 2021 09:00
-
-
Save syron/6d0c4fd80e0364cf9356a77aa6d7bbad to your computer and use it in GitHub Desktop.
linq.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace name_linq | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
List<string> names = new List<string>() | |
{ | |
"Robert", | |
"Winston", | |
"Leonard", | |
"Jesko", | |
"Paul", | |
"Christian", | |
"Ellie", | |
"Solveig", | |
"Stina", | |
"Emilio" | |
}; | |
foreach (var name in names) | |
{ | |
var firstDigit = name[0]; | |
Console.WriteLine(firstDigit); | |
} | |
Console.WriteLine("---------"); | |
foreach (var name in names) | |
{ | |
var firstDigit = name.First(); | |
Console.WriteLine(firstDigit); | |
} | |
Console.WriteLine("---------"); | |
names.ForEach(name => Console.WriteLine(name.First())); | |
Console.WriteLine("--------- Select"); | |
var firstDigitList = names.Select(name => name.First()); | |
foreach(var digit in firstDigitList) | |
{ | |
Console.WriteLine(digit); | |
} | |
Console.WriteLine("--------- Select + Foreach"); | |
names.Select(name => name.First()) | |
.ToList() | |
.ForEach(digit => Console.WriteLine(digit)); | |
Console.ReadLine(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment