2021-02-24 11:56:17 +11:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
2021-02-24 14:24:59 +11:00
|
|
|
|
using System.Linq;
|
2021-02-24 16:17:42 +11:00
|
|
|
|
using System.Reflection.Metadata;
|
2021-02-24 11:56:17 +11:00
|
|
|
|
|
|
|
|
|
namespace Features
|
|
|
|
|
{
|
|
|
|
|
class Program
|
|
|
|
|
{
|
|
|
|
|
// Modelling a company in this project
|
|
|
|
|
static void Main(string[] args)
|
|
|
|
|
{
|
2021-02-24 15:40:04 +11:00
|
|
|
|
/* Generic Delegate & Lambda Expression */
|
|
|
|
|
Func<int, int> square = x => x*x;// Define a generic delegate functions named square for square operation
|
|
|
|
|
Func<int, int, int> add = (x, y) =>
|
|
|
|
|
{
|
|
|
|
|
int temp = x + y;
|
|
|
|
|
return temp;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Action<int> write = x => Console.WriteLine(x);
|
|
|
|
|
write(square(add(3, 5)));
|
|
|
|
|
|
|
|
|
|
/* LINQ & C# */
|
|
|
|
|
|
2021-02-24 11:56:17 +11:00
|
|
|
|
IEnumerable<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"}
|
|
|
|
|
};
|
2021-02-24 13:49:26 +11:00
|
|
|
|
|
|
|
|
|
Console.WriteLine(sales.MyLinqCount());
|
2021-02-24 11:56:17 +11:00
|
|
|
|
|
|
|
|
|
var enumerator = developers.GetEnumerator();
|
|
|
|
|
|
|
|
|
|
while (enumerator.MoveNext())
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(enumerator.Current.Name); // .Current return value of current enumerator
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var person in developers)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine();
|
|
|
|
|
}
|
2021-02-24 14:24:59 +11:00
|
|
|
|
|
2021-02-24 15:40:04 +11:00
|
|
|
|
foreach (var employee in developers
|
|
|
|
|
.Where(e => e.Name.Length == 5)
|
|
|
|
|
.OrderBy(e => e.Name))
|
2021-02-24 14:24:59 +11:00
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(employee.Name);
|
|
|
|
|
}
|
2021-02-24 16:17:42 +11:00
|
|
|
|
|
|
|
|
|
// LINQ (Method syntax)
|
|
|
|
|
var query = developers.Where(e => e.Name.Length == 5)
|
|
|
|
|
.OrderBy(e => e.Name);
|
|
|
|
|
|
|
|
|
|
// LINQ (Query syntax)
|
|
|
|
|
var query2 = from developer in developers
|
|
|
|
|
where developer.Name.Length == 5
|
|
|
|
|
orderby developer.Name descending
|
|
|
|
|
select developer;
|
|
|
|
|
|
|
|
|
|
foreach (var employee in query)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(employee.Name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var employee in query2)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(employee.Name);
|
|
|
|
|
}
|
2021-02-24 11:56:17 +11:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|