static void Main(string[] args) { Func <int, int> square = x => x * x; Console.WriteLine(square(5)); int[] numbers = { 2, 3, 4, 5 }; var squaredNumbers = numbers.Select(x => x * x); Console.WriteLine(string.Join(" ", squaredNumbers)); Action line = () => Console.WriteLine(); line(); CalcMethod add = (a, b) => a + b; CalcMethod subtract = (a, b) => a - b; Console.WriteLine(add(10, 20)); Console.WriteLine(subtract(10.5, 20)); IsTeenAger isTeenAger = delegate(Student s) { return(s.Age > 12 && s.Age < 20); }; Student s1 = new Student() { Name = "John", Age = 18 }; Console.WriteLine("{0}은 {1}.", s1.Name, isTeenAger(s1) ? "청소년입니다" : "청소년이 아닙니다"); IsAdult isAdult = (s) => { int adultAge = 18; return(s.Age >= adultAge); }; Student s2 = new Student() { Name = "Robin", Age = 20 }; Console.WriteLine("{0}은 {1}.", s2.Name, isAdult(s2) ? "성인입니다" : "성인이 아닙니다"); }
/// <summary> /// The lambda expression is a shorter way of representing anonymous method using some special syntax. /// For example, following function checks if student is teenager or not /// </summary> public void TestLambdaBasicSyntax() { IsTeenAger isTeenAger = s => s.Age > 12 && s.Age < 20; Student stud = new Student() { Age = 25 }; Console.WriteLine(isTeenAger(stud)); }
private static void LambdaExpressionsEx() { IsTeenAger isTeenAger = s => s.Age > 12 && s.Age < 20; Student stud = new Student() { Age = 25 }; Console.WriteLine(isTeenAger(stud)); }
public static void Main() { IsTeenAger isTeenAger = s => s.Age > 12 && s.Age < 20; Student stud = new Student() { Age = 25 }; Console.WriteLine(isTeenAger(stud)); }
static void Main(string[] args) { // Example: Anonymous Method in C# IsTeenAger isTeenAger = delegate(Student s) { return(s.Age > 12 && s.Age < 20); }; Student stud = new Student() { Age = 25 }; Console.WriteLine(isTeenAger(stud)); }
public static void Main() { IsTeenAger isTeenAger = delegate(Student s) { return(s.Age > 12 && s.Age < 20); }; Student stud = new Student() { Age = 25 }; Console.WriteLine(isTeenAger(stud)); AnanymousStudent(); }
public static bool FuncLinq() { Student std = new Student(); //using delegate IsTeenAger isStudentTeen = s => s.Age > 12 && s.Age < 20; isStudentTeen(std); // false //using Func delegate has return and input Func <Student, bool> isStudentteenager = s => s.Age > 12 && s.Age < 20; bool isTeen = isStudentteenager(std); //false return(isTeen); }