Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /////////////////////// array.Sort
- var arr = new[]
- {
- new{X = 3, Y = 5},
- new{X = 3, Y = 6},
- new{X = 33, Y = 5},
- new{X = 34, Y = 55},
- new{X = 333, Y = 54},
- new{X = 33, Y = 6},
- new{X = 13, Y = 5},
- new{X = 21, Y = 16},
- };
- Array.Sort(arr,
- (a, b) => -a.Y.CompareTo(b.Y) == 0 ?
- a.X.CompareTo(b.X) :
- -a.Y.CompareTo(b.Y)
- );
- foreach (var item in arr)
- {
- Console.WriteLine(item);
- }
- Console.WriteLine();
- Console.WriteLine();
- Array.Sort(arr, (a, b) =>
- {
- if (a.Y == b.Y)
- {
- return a.X.CompareTo(b.X);
- }
- else
- {
- return a.Y.CompareTo(b.Y);
- }
- }
- );
- foreach (var item in arr)
- {
- Console.WriteLine(item);
- }
- ////////////////////// find all vsi4kite chetni chisla ///////////////////////
- List<int> list = new List<int>() { 1, 2, 3, 4 };
- List<int> evenNumbers = list.FindAll(x => (x % 2) == 0);
- foreach (var num in evenNumbers)
- {
- Console.Write("{0} ", num);
- }
- Console.WriteLine();
- // 2 4
- /////////////////////// remove all po-golemi ot 6 ///////////////////////////////////
- List<int> list = new List<int>() { 1, 2, 3, 4,5,6,7,8,9,0,12,-5 };
- list.RemoveAll(x => x > 6);
- foreach (var num in list)
- {
- Console.Write("{0} ", num);
- }
- Console.WriteLine();
- // 1 2 3
- /////////////////////// OrderBy pets po godini moje i po imena v azbuchen red ////////////////////////
- class Pet
- {
- public string Name { get; set; }
- public int Age { get; set; }
- }
- static void Main(string[] args)
- {
- var pets = new Pet[]
- {
- new Pet { Name="Sharo", Age=8 },
- new Pet { Name="Rex", Age=4 },
- new Pet { Name="Strela", Age=1 },
- new Pet { Name="Bora", Age=3 }
- };
- var sortedPets = pets.OrderBy(pet => pet.Age);
- foreach (Pet pet in sortedPets)
- {
- Console.WriteLine("{0} -> {1}",
- pet.Name, pet.Age);
- }
- ////////////// findAll che moga vutre v lambda expresinite da si otvarqm { } skobi i da si pishe algoritmi ////////
- list = new List<int>()
- { 20, 1, 4, 8, 9, 44 };
- // Process each argument with code statements
- evenNumbers = list.FindAll((i) =>
- {
- Console.WriteLine("value of i is: {0}", i);
- return (i % 2) == 0;
- });
- Console.WriteLine("Here are your even numbers:");
- foreach (int even in evenNumbers)
- Console.Write("{0} ", even);
- ////////////////// funk bool ////////////////////////////////////////////////
- Func<bool> boolFunc = () => true;
- Func<int, bool> intFunc = (x) => x < 10;
- if (boolFunc() && intFunc(5))
- {
- Console.WriteLine("5 < 10");
- }
- /////////////////// finAll i davame delegate vsi4ki suvpadashti na 'S' i da gi natrupva vuv lista townsWithS ///////
- List<string> towns = new List<string>()
- {
- "Sofia", "Plovdiv", "Varna", "Sopot", "Silistra"
- };
- List<string> townsWithS =
- towns.FindAll(delegate(string town)
- {
- return town.StartsWith("S");
- });
- // A short form of the above (with lambda expression)
- //List<string> townsWithS = towns.FindAll((town) => town.StartsWith("S"));
- foreach (string town in townsWithS)
- {
- Console.WriteLine(town);
- }
Advertisement
Add Comment
Please, Sign In to add comment