Created
July 13, 2015 05:21
-
-
Save bembengarifin/2ab295b1570c40194089 to your computer and use it in GitHub Desktop.
Generic & Lambda Expression
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; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace GenericTypeCollection | |
{ | |
class Car | |
{ | |
public string Model { get; set; } | |
public string PlateNumber { get; set; } | |
public int Age { get; set; } | |
} | |
class Person | |
{ | |
public string FirstName { get; set; } | |
public string LastName { get; set; } | |
public int Age { get; set; } | |
public string Gender { get; set; } | |
public Person() | |
{ | |
} | |
public Person(string firstName, string lastName, int age) | |
{ | |
FirstName = firstName; | |
LastName = lastName; | |
Age = age; | |
} | |
public override string ToString() | |
{ | |
return string.Format("{0} {1}, {2}", FirstName, LastName, Age); | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
//ArrayList arr = new ArrayList(); | |
//arr.Add(new Person { FirstName = "Jon", LastName = "Histed", Age = 35 }); | |
//arr.Add(new Person { FirstName = "Jane", LastName = "Doe", Age = 28 }); | |
//arr.Add(new Person { FirstName = "John", LastName = "Donald", Age = 34 }); | |
//arr.Add(new Person { FirstName = "Terry", LastName = "Cassidy", Age = 65 }); | |
//arr.Add(new Car { Model = "Mustang", PlateNumber = "S 888" }); | |
//ArrayList filtered = new ArrayList(); | |
//foreach (Person item in arr) | |
//{ | |
// if (item.Age < 30) | |
// filtered.Add(item); | |
//} | |
//foreach (Person item in filtered) | |
//{ | |
// Console.WriteLine(item); | |
//} | |
//arr.Add(new Person(age: 23, firstName: "Terry", lastName: "Cassidy")); | |
//arr.Add(new Person("Terry", "Cassidy", 35)); | |
//arr.Add(new Person("Terry", "Cassidy", 35) { Gender = "Male" }); | |
//foreach (Person item in arr) | |
//{ | |
// Console.WriteLine(item.FirstName); | |
//} | |
List<Person> arr = new List<Person>(); | |
arr.Add(new Person { FirstName = "Jon", LastName = "Histed", Age = 35 }); | |
arr.Add(new Person { FirstName = "Jane", LastName = "Doe", Age = 28 }); | |
arr.Add(new Person { FirstName = "John", LastName = "Donald", Age = 34 }); | |
arr.Add(new Person { FirstName = "Terry", LastName = "Cassidy", Age = 65 }); | |
//arr.Add(new Car { Model = "Mustang", PlateNumber = "S 888" , Age = 4}); | |
//var filtered = arr.Where(p => p.Age < 30); | |
//Predicate<Person> pre = p => p.Age < 50; | |
//var results = Array.FindAll(arr.ToArray(), pre); | |
//Func<Person, bool> filter = p => | |
//{ | |
// return p.Age > 50; | |
//}; | |
//var filtered = arr.Where(filter); | |
//foreach (var item in filtered) | |
//{ | |
// Console.WriteLine(item); | |
//} | |
//Action<Person> act = p => Console.WriteLine(p); | |
//Action<Person> act = delegate (Person p) | |
//{ | |
// Console.WriteLine(p); | |
//}; | |
//act(new Person("James", "Bond", 45)); | |
//Console.WriteLine(filter(new Person { Age = 44 })); | |
//Func<int, int, int> calc = (i1, i2) => i1 * i2; | |
//Console.WriteLine(calc(5, 6)); | |
//Func<int, int, int> division = delegate (int i1, int i2) { return i1 / i2; }; | |
//Console.WriteLine(division(10, 5)); | |
//var filtered = arr.Where(p => p.Age < 30).ToList(); | |
//var filtered = (from p in arr.Where(x => x.FirstName.StartsWith("J")) | |
// where p.Age > 30 | |
// select new { p.FirstName, p.Age }); | |
//arr.Add(new Person { FirstName = "Jack", LastName = "Yer", Age = 25 }); | |
//foreach (var item in filtered) | |
//{ | |
// Console.WriteLine(item); | |
//} | |
//DoSomething(delegate (string msg) { Console.WriteLine(msg); }); | |
//DoSomethingWithAction(delegate (string msg) { Console.WriteLine(msg); }); | |
//Action<String, int, | |
//DoCalculation((i1, i2) => i1 * i2); | |
//DoCalculation((i1, i2) => i1 - i2); | |
//DoCalculation(delegate (int i1, int i2) { return i1 + i2; }); | |
List<Action> acts = new List<Action>(); | |
for (int i = 0; i < 5; i++) | |
{ | |
var locali = i; | |
acts.Add(() => Console.WriteLine(locali)); | |
} | |
foreach (var act in acts) | |
{ | |
act(); | |
} | |
Console.WriteLine("Done"); | |
Console.Read(); | |
} | |
public static void DoSomething(DoSomethingInAdditional del) | |
{ | |
Console.WriteLine("Doing something internally"); | |
del("additional stuffs"); | |
} | |
public static void DoSomethingWithAction(Action<String> del) | |
{ | |
Console.WriteLine("Doing something with action internally"); | |
del("additional stuffs with action"); | |
} | |
public static void DoCalculation(Func<int, int, int> calc) | |
{ | |
Console.WriteLine("Doing something with action internally"); | |
var i1 = 10; | |
var i2 = 4; | |
Console.WriteLine(calc(i1, i2));; | |
} | |
public delegate void DoSomethingInAdditional(string message); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment