static void Main(string[] args) { //Foundations IEnumerable <Employee> devs = new Employee[] { new Employee() { Id = 1, Name = "Manoj" }, new Employee() { Id = 2, Name = "Nandan" } }; IEnumerable <Employee> sales = new List <Employee> { new Employee() { Id = 3, Name = "Saranya" } }; IEnumerator <Employee> enumerator = devs.GetEnumerator(); while (enumerator.MoveNext()) { Console.WriteLine($"{enumerator.Current.Id,5}: {enumerator.Current.Name,-10}"); } Console.WriteLine(devs.CountKaro()); //Funct Func <int, int> sqr = Square; Console.WriteLine(sqr(10)); }
static void Main(string[] args) { Employee[] developers = new Employee[] { new Employee { Id = 1, Name = "Scott" }, new Employee { Id = 2, Name = "Chris" } }; List <Employee> sales = new List <Employee>() { new Employee { Id = 3, Name = "Alex" } }; IEnumerator <Employee> enumerator = (IEnumerator <Employee>)developers.GetEnumerator(); while (enumerator.MoveNext()) { Console.WriteLine(enumerator.Current?.Name); } }
static void Main(string[] args) { IEnumerable <Employee> developers = new Employee[] { new Employee { Id = 1, Name = "Rafa" }, new Employee { Id = 2, Name = "Roger" }, new Employee { Id = 2, Name = "Andy" } }; IEnumerable <Employee> Sales = new List <Employee>() { new Employee { Id = 3, Name = "Djokovic" } }; int cntEmpl = developers.Count(); Console.WriteLine(cntEmpl); IEnumerator <Employee> enumerator = developers.GetEnumerator(); foreach (var emp in developers.Where(e => e.Name.StartsWith("R"))) { Console.WriteLine(emp.Name); } Console.ReadLine(); }
static void Main(string[] args) { // Func indicates lambda definition // takes an int and return an int Func <int, int> square = x => x * x; Console.WriteLine(square(3)); Console.WriteLine("**********"); Func <int, int, int> add = (x, y) => x + y; Console.WriteLine(add(3, 4)); Console.WriteLine("**********"); Func <int, int, int> add2 = (x, y) => { int temp = x + y; return(temp); }; Console.WriteLine("**********"); // returns void Action <int> write = x => Console.WriteLine(x); write(999); Console.WriteLine("**********"); IEnumerable <Employee> developers = new Employee[] { new Employee { Id = 1, Name = "Julien" }, new Employee { Id = 2, Name = "Bibi" } }; Console.WriteLine(developers.MyCount()); Console.WriteLine("**********"); IEnumerable <Employee> sales = new List <Employee>() { new Employee { Id = 3, Name = "Lulu" } }; IEnumerator <Employee> enumerator = developers.GetEnumerator(); while (enumerator.MoveNext()) { Console.WriteLine(enumerator.Current.Name); } Console.WriteLine("**********"); foreach (var Employee in developers.Where(e => e.Name.StartsWith('B'))) { Console.WriteLine(Employee.Name); } }
static void Main(string[] args) { //using func type used more Func <int, int> square = x => x * x; Func <int, int, int> add = (x, y) => x + y; //using action type used less than func types // always returns void Action <int> write = x => Console.WriteLine(x); write(square(add(3, 5))); //creating an array of Employee using IEnumerable IEnumerable <Employee> developers = new Employee[] { new Employee { Id = 1, Name = "Scott" }, new Employee { Id = 2, Name = "Chris" } }; IEnumerable <Employee> sales = new List <Employee>() { new Employee { Id = 3, Name = "Alex" } }; Console.WriteLine(developers.Count()); // Using only method of IEnumerable,GetEnumerator, that allows us to iterate // through and collection of things IEnumerator <Employee> enumerator = developers.GetEnumerator(); while (enumerator.MoveNext()) { Console.Write(enumerator.Current.Name); } // Complicated way of filtering without lambda foreach (var employee in developers.Where( delegate(Employee employee) { return(employee.Name.StartsWith("S")); })) { Console.WriteLine(employee.Name); } //instead use lambda expression to simplify above expression foreach (var employee in developers.Where( e => e.Name.StartsWith("S"))) { Console.WriteLine(employee.Name); } }
static void Main(string[] args) { Func <int, int> square = x => x * x; Func <int, int, int> add = (x, y) => { int temp = x + y; return(temp); }; Action <int> print = x => Console.WriteLine(x); print(square(add(5, 5))); Console.WriteLine("********"); IEnumerable <Employee> developers = new Employee[] { new Employee() { Id = 1, Name = "Outmane" }, new Employee() { Id = 2, Name = "Hamza" } }; IEnumerable <Employee> sales = new List <Employee>() { new Employee() { Id = 3, Name = "Mehdi" } }; Console.WriteLine(developers.Count()); Console.WriteLine("******************"); IEnumerator <Employee> enumerator = developers.GetEnumerator(); while (enumerator.MoveNext()) { Console.WriteLine(enumerator.Current.Name); } Console.WriteLine("******************"); //var query = developers.Where(e => e.Name.Length == 5) // .OrderBy(e => e.Name); var query = from developer in developers where developer.Name.Length == 5 orderby developer.Name select developer; foreach (var employee in query) { Console.WriteLine(employee.Name); } }
static void Main(string[] args) { IEnumerable <Employee> developers = new Employee[] { new Employee { Id = 1, Name = "Scott" }, new Employee { Id = 2, Name = "Chris" } }; IEnumerable <Employee> sales = new List <Employee>() { new Employee { Id = 3, Name = "Alex" } }; Console.WriteLine(developers.Count()); //Here Count is the Custom Extension method made in MyLinq Console.WriteLine(sales.Count()); IEnumerator <Employee> enumerator = developers.GetEnumerator(); while (enumerator.MoveNext()) { Console.WriteLine(enumerator.Current.Name); } /**Method Syntax Approach**/ //Printing names starting with 'S' foreach (var employee in developers.Where(employee => employee.Name.StartsWith("S"))) { Console.WriteLine(employee.Name); } //Printing names having length 5 and sorting it foreach (var employee in developers.Where(employee => employee.Name.Length == 5).OrderBy(e => e.Name)) { Console.WriteLine(employee.Name); } /**Query Syntax Approach**/ var query2 = from developer in developers where developer.Name.Length == 5 orderby developer.Name descending select developer; foreach (var dev in query2) { Console.WriteLine(dev.Name); } }
static void Main(string[] args) { Func <int, int> sequre = sequreFunction; Func <int, int> sequre2 = number => number * number; Func <int, int, int> Add = (x, y) => x + y; Action <string> write = x => Console.WriteLine(x); Action <string, string> writeFullName = (FName, LName) => Console.WriteLine($"Full Name is : {FName} {LName}"); Console.WriteLine(sequre(10)); Console.WriteLine(sequre2(10)); Console.WriteLine(sequre(Add(10, 20))); write("called Action"); writeFullName("Jassar", "Mahmoud"); IEnumerable <Employee> developers = new Employee[] { new Employee { Id = 1, Name = "Jassar" }, new Employee { Id = 2, Name = "Mahmoud" } }; IEnumerable <Employee> sales = new List <Employee>() { new Employee { Id = 3, Name = "Jaffar" } }; IEnumerator <Employee> enumerator = developers.GetEnumerator(); Console.WriteLine(developers.Count()); while (enumerator.MoveNext()) { Console.WriteLine(enumerator.Current.Name); } }
static void Main(string[] args) { Func <int, int> squares = x => x * x; Action <int> action = x => Console.WriteLine(x); action(squares(4)); IEnumerable <Employee> developers = new Employee[] { new Employee { Id = 1, Name = "Dudu" }, new Employee { Id = 2, Name = "Hagu" } }; IEnumerable <Employee> sales = new List <Employee>() { new Employee { Id = 3, Name = "BoyalMach" } }; Console.WriteLine($"Number of developers: {developers.Count()}"); IEnumerator <Employee> enumerator = developers.GetEnumerator(); var query = developers.Where(d => d.Name.Length == 4) .OrderBy(d => d.Name); var query2 = from developer in developers where developer.Name.Length == 4 orderby developer.Name select developer; while (enumerator.MoveNext()) { Console.WriteLine(enumerator.Current.Name); } Console.ReadKey(); // Go to http://aka.ms/dotnet-get-started-console to continue learning how to build a console app! }
static void Main(string[] args) { IEnumerable <Employee> developers = new Employee[] // Employee[] developers = new Employee[] { new Employee { Id = 1, Name = "Scott" }, new Employee { Id = 2, Name = "Chris" } }; IEnumerable <Employee> sales = new List <Employee>() //List <Employee> sales = new List<Employee>() { new Employee { Id = 3, Name = "Alex" } }; //foreach statment behide the scene IEnumerator <Employee> enumerator = developers.GetEnumerator(); while (enumerator.MoveNext()) { Console.WriteLine(enumerator.Current.Name); } /* * foreach (var person in sales) * { * Console.WriteLine(person.Name); * } */ }
static void Main(string[] args) { Func <int, int> square = x => x * x; // last int is type for return Console.WriteLine(square(3)); Func <int, int, int> add = (x, y) => x + y; Func <int, int, int> add2 = (x, y) => { int z = x + y; return(z); }; Console.WriteLine(square(add(3, add2(2, 2)))); Action <int> write = x => Console.WriteLine(x); write(square(add(3, add2(2, 2)))); Console.WriteLine("*****************"); IEnumerable <Employee> developers = new Employee[] { new Employee { Id = 1, Name = "Scott" }, new Employee { Id = 2, Name = "Chris" } }; IEnumerable <Employee> sales = new List <Employee>() { new Employee { Id = 3, Name = "Alex" } }; Console.WriteLine(sales.Count()); IEnumerator <Employee> enumerator = developers.GetEnumerator(); while (enumerator.MoveNext()) { Console.WriteLine(enumerator.Current.Name); } Console.WriteLine("*****************"); foreach (var item in developers.Where(NameStartsWithS)) { Console.WriteLine(item.Name); } Console.WriteLine("*****************"); foreach (var item in developers.Where(delegate(Employee arg) { return(arg.Name.StartsWith("S")); })) { Console.WriteLine(item.Name); } Console.WriteLine("*****************"); foreach (var item in developers.Where(arg => arg.Name.StartsWith("S"))) { Console.WriteLine(item.Name); } Console.WriteLine("*****************"); foreach (var item in developers.Where(arg => arg.Name.Length == 5) .OrderBy(e => e.Name)) { Console.WriteLine(item.Name); } Console.WriteLine("*****************"); var query = developers.Where(arg => arg.Name.Length == 5) .OrderBy(e => e.Name); foreach (var item in query) { Console.WriteLine(item.Name); } Console.WriteLine("*****************"); var query2 = from dev in developers where dev.Name.Length == 5 orderby dev.Name select dev; foreach (var item in query2) { Console.WriteLine(item.Name); } }
static void Main(string[] args) { IEnumerable <Employee> developers = new Employee[] { new Employee { Id = 1, Name = "Scott" }, new Employee { Id = 2, Name = "Chris" }, new Employee { Id = 3, Name = "Katie" }, new Employee { Id = 4, Name = "Bob" } }; IEnumerable <Employee> sales = new List <Employee>() { new Employee { Id = 3, Name = "Alex" } }; foreach (var person in sales) { Console.WriteLine(person.Name); } // Check extension method operation Console.WriteLine(developers.Count()); // For loop using enumerator IEnumerator <Employee> enumerator = developers.GetEnumerator(); while (enumerator.MoveNext()) { Console.WriteLine(enumerator.Current.Name); } // ----- List all developers whose name starts with S ----- // Named method Console.WriteLine("Names start with S - Named Method:"); foreach (var employee in developers.Where(NameStartsWithS)) { Console.WriteLine(employee.Name); } // Anonymous method Console.WriteLine("Names start with C - Anonymous Method:"); foreach (var employee in developers.Where( delegate(Employee employee) { return(employee.Name.StartsWith("C")); })) { Console.WriteLine(employee.Name); } // Lambda expression Console.WriteLine("Names start with K - Lambda Expression:"); foreach (var employee in developers.Where(e => e.Name.StartsWith("K"))) { Console.WriteLine(employee.Name); } // Func type - named method Func <int, int> funcNamed = Square; Console.WriteLine("Func type - named method:\t3^2 = {0}", funcNamed(3)); // Func type - lambda expression Func <int, int> funcLambda = x => x * x; Console.WriteLine("Func type - lambda exp:\t\t3^2 = {0}", funcLambda(3)); // Func with two input parameters Func <int, int, int> add = (x, y) => x + y; Console.WriteLine("Func w/ 2 inputs:\t\t3 + 5 = {0}", add(3, 5)); // Action type Action <int> write = x => Console.WriteLine(x); Console.WriteLine("Action:"); write(54); // Order developers by name Console.WriteLine("Names with 5 letters, ordered by name, method syntax:"); foreach (var employee in developers.Where(e => e.Name.Length == 5) .OrderBy(e => e.Name)) { Console.WriteLine(employee.Name); } // Query syntax var query = from e in developers where e.Name.Length == 5 orderby e.Name select e; Console.WriteLine("Names with 5 letters, ordered by name, query syntax:"); foreach (var employee in query) { Console.WriteLine(employee.Name); } }
static void Main(string[] args) { //the Func<> delegate type takes generic type parameters defined in the <> //the last parameter is the return type Func <int, bool> isEven = x => x % 2 == 0; //returns a bool Func <int, int> square = x => x * x; //returns an int //does not need () as has 0 or 1 parameters Func <int, int, int> add = (x, y) => x + y; //returns an int //needs () to define parameters - (int x, int y) Console.WriteLine($"22 isEven: {isEven(22)}"); Console.WriteLine($"3 squared is: {square(3)}"); Console.WriteLine($"7 plus 8 is: {add(7, 8)}"); Console.WriteLine($"The square of 7 + 9 is: {square(add(7, 9))}"); //The Action delegate type is similar to Func<>, only it always returns void Action <string, string> insult = (first, second) => Console.WriteLine($"{first} {second}"); insult("you", "smell"); IEnumerable <Employee> developers = new Employee[] { new Employee { Id = 1, Name = "Ciaran" }, new Employee { Id = 2, Name = "Pooty" } }; List <Employee> sales = new List <Employee>() { new Employee() { Id = 3, Name = "Pam" } }; //This is basically what collections that implement IEnumerable<T> do when using for/foreach IEnumerator <Employee> enumerator = developers.GetEnumerator(); while (enumerator.MoveNext()) { Console.WriteLine(enumerator.Current.Name); } //This uses an extension method off of IEnumerable<T> that we have created Console.WriteLine(developers.Count()); //Use 'named method' in Where() foreach (var developer in developers.Where(NameStartsWithP)) { Console.WriteLine("Named Method:"); Console.WriteLine(developer.Name); } //use 'anonymous method' - i.e. delegate method - in Where() foreach (var developer in developers.Where( delegate(Employee employee) { return(employee.Name.StartsWith("P")); })) { Console.WriteLine("Delegate Method:"); Console.WriteLine(developer.Name); } //use 'lambda method' in Where() foreach (var developer in developers.Where( e => e.Name.StartsWith("P"))) { Console.WriteLine("Lambda Method:"); Console.WriteLine(developer.Name); } //use lambda expression to use a full LINQ query using lambda expressions foreach (var developer in developers.Where(e => e.Name.Length == 6) .OrderBy(e => e.Name.StartsWith("C"))) { Console.WriteLine("Developers with 6 letter names beginning with C:"); Console.WriteLine(developer.Name); } //LINQ query - 'method syntax' var query = developers.Where(e => e.Name.Length == 5) .OrderBy(e => e.Name); string[] cities = { "Boston", "Los Angeles", "Seattle", "London", "Hyderabad" }; //LINQ query syntax IEnumerable <String> filteredCities1 = from city in cities where city.StartsWith("L") && city.Length < 15 orderby city select city; //LINQ method syntax IEnumerable <String> filteredCities2 = cities.Where(c => c.StartsWith("L") && c.Length < 15) .OrderBy(c => c); }
static void Main(string[] args) { Func <int, int> square = x => x * x; Console.WriteLine(square(3)); Console.WriteLine("+++++++++"); Func <int, int, int> add = (x, y) => x + y; Console.WriteLine(square(add(3, 5))); IEnumerable <Employee> developers = new Employee[] { new Employee { Id = 1, Name = "Scott" }, new Employee { Id = 2, Name = "Chris" }, new Employee { Id = 3, Name = "Alexa" } }; Console.WriteLine("+++++++++"); IEnumerable <Employee> sales = new List <Employee>() { new Employee { Id = 3, Name = "Alex" } }; Console.WriteLine(developers.Count()); IEnumerator <Employee> enumerator = developers.GetEnumerator(); Console.WriteLine("+++++++++"); while (enumerator.MoveNext()) { Console.WriteLine(enumerator.Current.Name); } Console.WriteLine("+++++++++"); foreach (var employee in developers.Where( e => e.Name.StartsWith("C"))) { Console.WriteLine(employee.Name); } Console.WriteLine("+++++++++"); //method syntax var query1 = developers.Where(e => e.Name.Length == 5) .OrderByDescending(e => e.Name) .Select(e => e); Console.WriteLine("+++++++++"); //query syntax var query2 = from developer in developers where developer.Name.Length == 5 orderby developer.Name descending select developer;; foreach (var employee in query1) { Console.WriteLine(employee.Name); } }
static void Main(string[] args) { #region Region7 - Functional programming var duration = Time(() => TestMethod()); Console.WriteLine($"Elapsed in Milliseconds: {duration}"); Console.WriteLine("**********"); Console.WriteLine("\r"); #endregion /*******************************************/ #region Region6 - Peek extension method to get the results by steps Employee[] developers5 = new Employee[] { new Employee { Id = 1, Name = "Juan" }, new Employee { Id = 2, Name = "Jose" }, new Employee { Id = 3, Name = "Antonio" } }; var query3 = developers5.Where(e => e.Name.Length > 3) .Peek(e => Console.WriteLine($"Step 1: {e.Name}")) .OrderBy(e => e.Name) .Peek(e => Console.WriteLine($"Step 2: {e.Name}")); //var query3 = Enumerable.Range(1, 10) // .Select(n => n * n) // .Peek(n => Console.WriteLine($"Step 1: {n}")) // .Select(n => n / 2) // .Peek(n => Console.WriteLine($"Step 2: {n}")) // .Select(n => n - 5); foreach (var item in query3) { Console.WriteLine($"Output: {item.Name}"); } Console.WriteLine("**********"); Console.WriteLine("\r"); #endregion /*******************************************/ #region Region5 - Method syntax and Query syntax Employee[] developers4 = new Employee[] { new Employee { Id = 1, Name = "Juan" }, new Employee { Id = 2, Name = "Jose" }, new Employee { Id = 3, Name = "Antonio" } }; var query = developers4.Where(e => e.Name.Length == 7) .OrderBy(e => e.Name); //.Select(e => e.Name); // The Select is optional in this case foreach (var person in query) { Console.WriteLine("METHOD SYNTAX"); Console.WriteLine(person.Name); } var query2 = from developer in developers4 where developer.Name.Length == 7 orderby developer.Name select developer; foreach (var person in query2) { Console.WriteLine("QUERY SYNTAX"); Console.WriteLine(person.Name); } Console.WriteLine("**********"); Console.WriteLine("\r"); #endregion /*******************************************/ #region Region4 Action - Takes 0 or more parameters and returns void Func <int, int> square2 = x => x * x; Action <int> write = x => Console.WriteLine(x); Console.WriteLine("ACTION"); write(square2(3)); #endregion /*******************************************/ #region Region4 Func - Takes 0 or more parameters and returns a value Func <int, int> square = x => x * x; Func <int, int, int> add = (x, y) => x + y; Func <int, int, int> add2 = (x, y) => { int temp = x + y; return(temp); }; Console.WriteLine("LAMBDA EXPRESSION"); Console.WriteLine(square(3)); Console.WriteLine(square(add(3, 5))); // On the left hand side of the lambda we have the parameters list (in this case "x") // On the right hand side of the lambda we have the expression that returns a value (in this case "x * x") Func <int, int> f1 = x => x * x; Func <int, int> f = Square; Console.WriteLine("NAME METHOD"); Console.WriteLine(f(3)); Console.WriteLine("**********"); Console.WriteLine("\r"); //Console.WriteLine("\r\n"); #endregion /*******************************************/ #region Region3 Employee[] developers3 = new Employee[] { new Employee { Id = 1, Name = "Juan" }, new Employee { Id = 2, Name = "Jose" }, new Employee { Id = 3, Name = "Carlos" } }; foreach (var person in developers3.Where(e => e.Name.StartsWith("C"))) { Console.WriteLine("LAMBDA EXPRESSION"); Console.WriteLine(person.Name); } foreach (var person in developers3.Where( delegate(Employee employee) { Console.WriteLine("ANONYMOUS METHOD"); return(employee.Name.StartsWith("C")); })) { Console.WriteLine(person.Name); } foreach (var person in developers3.Where(NameStartsWithC)) { Console.WriteLine("NAME METHOD"); Console.WriteLine(person.Name); } Console.WriteLine("**********"); Console.WriteLine("\r"); #endregion /*******************************************/ #region Region2 IEnumerable <Employee> developers2 = new Employee[] { new Employee { Id = 1, Name = "Juan" }, new Employee { Id = 2, Name = "Jose" } }; // Otra forma de hacer foreach IEnumerator <Employee> enumeratorDevelopers2 = developers2.GetEnumerator(); while (enumeratorDevelopers2.MoveNext()) { Console.WriteLine(enumeratorDevelopers2.Current.Name); } foreach (var person in developers2) { Console.WriteLine(person.Name); } IEnumerable <Employee> sales2 = new List <Employee> { new Employee { Id = 3, Name = "Pedro" }, new Employee { Id = 4, Name = "Pablo" } }; // Otra forma de hacer foreach IEnumerator <Employee> enumeratorSales2 = sales2.GetEnumerator(); while (enumeratorSales2.MoveNext()) { Console.WriteLine(enumeratorSales2.Current.Name); } // Una forma de llamar al extension method Console.WriteLine(MyLinq.Count(sales2)); // Otra forma de llamar al extension method Console.WriteLine(sales2.Count()); foreach (var person in sales2) { Console.WriteLine(person.Name); } Console.WriteLine("**********"); Console.WriteLine("\r"); #endregion /*******************************************/ #region Region1 Employee[] developers = new Employee[] { new Employee { Id = 1, Name = "Juan" }, new Employee { Id = 2, Name = "Jose" } }; foreach (var person in developers) { Console.WriteLine(person.Name); } List <Employee> sales = new List <Employee> { new Employee { Id = 3, Name = "Pedro" }, new Employee { Id = 4, Name = "Pablo" } }; foreach (var person in sales) { Console.WriteLine(person.Name); } Console.WriteLine("**********"); Console.WriteLine("\r"); #endregion /*******************************************/ // Keep the console window open in debug mode. Console.WriteLine("Press any key to exit."); Console.ReadKey(); }
static void Main(string[] args) { // delegate types //"Func" type parameters - the last will always be the return value Func <int, int> square = x => x * x; Func <int, int, int> add = (x, y) => x + y; // "Action" always returns void, type paramaters are just describing the function parameters Action <int> write = x => Console.WriteLine(x); Console.WriteLine(square(add(3, 5))); IEnumerable <Employee> developers = new Employee[] { new Employee { Id = 1, Name = "Scott" }, new Employee { Id = 2, Name = "Chris" } }; IEnumerable <Employee> sales = new List <Employee>() { new Employee { Id = 3, Name = "Alex" } }; // method syntax of LINQ var query = developers.Where(e => e.Name.Length == 5) .OrderBy(e => e.Name); // query syntax of LINQ var query2 = from developer in developers where developer.Name.Length == 5 orderby developer.Name select developer; // Using an Extension method Console.WriteLine(developers.Count()); // Most datastructures derive from the IEnmurator interface // This meves they have a "GetEnumerator" method that describes how to enumerate through the datastructure // Below is an example of directly using that enumerator // It will work on both the Array and the List above // "foreach" works on different datastructures because it uses their individual enumerator behind the scences IEnumerator <Employee> enumerator = developers.GetEnumerator(); while (enumerator.MoveNext()) { Console.WriteLine(enumerator.Current.Name); } // lambda expressions foreach (var employee in developers.Where(e => e.Name.StartsWith("S"))) { Console.WriteLine(employee.Name); } foreach (var employee in developers.Where(e => e.Name.Length == 5) .OrderBy(e => e.Name)) { Console.WriteLine(employee.Name); } }