static void Main(string[] args)
        {
            //args => expression
            //number => number*number;

            // ()=> ...//eğer hiç arguman olmayacak ise
            // x => ... //tek argüman alacak ise
            // (x,y,z) => ..... //birden çok argüman alacak ise

            Func<int, int> square = number => number * number; //Square;
            Console.WriteLine(square(5));

            //Console.WriteLine(Square(5));
            //Console.ReadLine();

            //Lambda içinde aynı scope içinde başka değişkende kullanılabilir (factor) gibi
            const int factor = 5;
            Func<int, int> multipler = n => n * factor;
            var result = multipler(10);

            Console.WriteLine(result);
            //Console.ReadLine();

            var books = new BookRepository().GetBooks();

            //var cheapBooks = books.FindAll(IsCheaperThan10Dollars); //predicate method ile kullanımı
            var cheapBooks = books.FindAll(b => b.Price < 10); //LambdaExpression hali

            foreach (var book in cheapBooks)
            {
                Console.WriteLine(book.Title);
            }
            Console.ReadLine();
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            var books = new BookRepository().GetBooks();

            var cheapBooks = books.FindAll(b => b.Price < 10);

            foreach (var book in cheapBooks)
            {
                Console.WriteLine(book.Title);
            }
        }
Beispiel #3
0
        static void LambdaExpressions()
        {
            var lambdaExamples = new LambdaExamples();

            var books          = new LambdaExpressions.Book();
            var bookRepository = new LambdaExpressions.BookRepository().GetBooks();

            var cheapBooks = bookRepository.FindAll(books.IsCheaperThanTenDollars);

            foreach (var book in cheapBooks)
            {
                Console.WriteLine(book.Title);
            }

            // This Lambda Expression is exactly the same as the above code
            var cheapBooksLambda = bookRepository.FindAll(book => book.Price < 10);

            foreach (var book in cheapBooksLambda)
            {
                Console.WriteLine(book.Title);
            }
        }
Beispiel #4
0
        static void Main(string[] args)
        {

            //anonymous method
            //no access modifer
            //no name
            //no return statement.

            //why use anonymous method? for convenience

            //example:

            //takes a number and returns a square of that number...
            //typically you would do this (see square method)

            Console.WriteLine(Square(5));

            //lambda example

            //syntax  

            //args => expression
            //args goes to expression

            //number => number * number;

            //to get the 'return' action we have to assign it to a delegate
            //we use func because we want to return a value
            //in the func the first argument is the argument type for the method and 2nd is the datastype for the return value

            Func<int, int> square = number => number * number;



            //calling the delegate here
            Console.WriteLine(square(9));

            //if you don't need any arguments you write a lambda expression like this:
            //() => ...

            //1 argument
            // x => ...

            //multiple args
            //(x,y,z) => ...

            //scope - lambda has access to variables passed and anythign defined in the function the lambda was created in.

            const int factor = 5;

            Func<int, int> multiplier = n => n * factor;
            Console.WriteLine(multiplier(6));

            //more practical and common usecase of lambda expression

            //write code to return all books that are less than 10 dollars
            //traditional way

            var books = new BookRepository().GetBooks();


            //the findall function gets a predicate method passed
            //predicate is a deleagate pointing to a function.
            //this basically iterates through the method and return it if the condition is sastisfied in the results.

            var cheapBooks = books.FindAll(IsCheaperThan10Dollars);

            foreach (var book in cheapBooks)
            {
                Console.WriteLine(book.Title);
            }



            //now let's the above with a lambda

            //the problem is everytime you want to call find you have to go create a method etc.
            //with a lamdba you can do this:

            var cheapBooks2 = books.FindAll(book => book.Price < 10);

            //in c# it's pretty common to just use a single letter with the lamdba arguments.
            //in the exmaple above we are working with books you could change the line above to

            //var cheapBooks2 = books.FindAll(b => b.Price < 10);

            Func <int, int> DoAdd = addme => addme + 5;
            Console.WriteLine(DoAdd(5));

        }