static void Main(string[] args) { /* * Console.WriteLine(Square(7)); * * Func<int, int> squareLambda = number => number * number; * Console.WriteLine(squareLambda(11)); * * const int factor = 5; * Func<int, int> multiplier = n => n * factor; * Console.WriteLine(multiplier(10)); */ var books = new BookRepository().GetBooks(); // Find cheap books without a lambda expression: var cheapBooks = books.FindAll(IsCheaperThan10Dollars); foreach (var book in cheapBooks) { Console.WriteLine(book.Title); } // Find cheap books using a lambda expression: var cheapBooksLambda = books.FindAll(b => b.Price < 10); foreach (var book in cheapBooksLambda) { Console.WriteLine("Lambda Result: " + book.Title); } }
// Exemplo 2 private static void SecondExample() { var books = new BookRepository().GetBooks(); // A Predicate is basically a delegate which points to a method that // gets a Book (in this case) and returns a boolean value specifying // if a given condition is satisfied // this is what I always did, but simplified. I finally understand // lambda functions Console.WriteLine("Cheapest books by passing a normal function to the delegate"); var cheapBooksNormalFunction = books.FindAll(IsCheaperThan10Dollars); foreach (var book in cheapBooksNormalFunction) { Console.WriteLine(book.Title); } Console.WriteLine("Cheapest books by passing a lambda function to the delegate"); var cheapBooksLambda = books.FindAll(book => book.Price < 10); foreach (var book in cheapBooksLambda) { Console.WriteLine(book.Title); } }
static void Main(string[] args) { Console.WriteLine(Square(5)); // arguments =>(goes to) expression // () => ... // x => ... // (x, y, z) => ... // return from delegate Func <int, int> square = number => number * number; Console.WriteLine(square(5)); const int factor = 5; Func <int, int> multiplier = n => n * factor; Console.WriteLine(multiplier(10)); // return from local function int Square(int number) => number * number; Console.WriteLine(Square(5)); int Add(int a, int b) => a + b; Console.WriteLine(Add(5, 5)); // BookRepository class example. Find books cheaper then 10 // 1 var books = new BookRepository().GetBooks(); var cheapBooks1 = new List <Book>(); foreach (var book in books) { if (book.Price > 10) { continue; } cheapBooks1.Add(book); Console.WriteLine(book.Title); } // 2 var cheapBooks2 = books.FindAll(IsCheaperThan10Dollars); foreach (var book in cheapBooks2) { Console.WriteLine(book.Title); } // 3 var cheapBooks3 = books.FindAll(book => book.Price < 10); foreach (var book in cheapBooks3) { Console.WriteLine(book.Title); } }
static void Main(string[] args) { // args => expression // number => number * number // <args, retval> Func <int, int> square = num => num * num; Console.WriteLine(square(5)); // no args () => ... // x => ... // (x, y, z) => ... const int factor = 5; Func <int, int> mult = n => n * factor; Console.WriteLine(mult(10)); var books = new BookRepository().GetBooks(); var cheapBooks = books.FindAll(b => b.Price < 10); foreach (var book in cheapBooks) { Console.WriteLine(book.Title); } }
static void Main(string[] args) { // arfs => expression // x => ... //(x, y, z) => ... //delegate Func<> Func <int, int> square = number => number * number; //number => number * number---lambda expression Console.WriteLine(square(5)); const int factor = 6; Func <int, int> multiplier = n => n * factor; var result = multiplier(10); Console.WriteLine(result); //Book Example var books = new BookRepository().GetBooks(); //Predicate<T>delegate match means that returns a boolean var cheapBooks = books.FindAll(b => b.Price < 10); foreach (var book in cheapBooks) { Console.WriteLine(book.Title); } }
static void Main(string[] args) { // () => .... // x => .... //(x, y, z) => .... Func <int, int> square = num => num * num; int number = 5; Console.WriteLine(square(number)); Func <int, int, int> multiplier = (m, n) => n * (m * Factor); var result = multiplier(10, 1); Console.WriteLine(result); //Using book repository var books = new BookRepository().GetBooks(); var cheapBooks = books.FindAll(b => b.Price < 10); foreach (var book in cheapBooks) { Console.WriteLine(book.Title); } Console.ReadKey(); }
static void Main(string[] args) { /* * //args => expression * * // () => ... * // x = > ... * // (x, y, z) => ... * * * const int factor = 5; * Func<int, int> multiplier = n => n * factor; * * var result = multiplier(10); * Console.WriteLine(result); */ var books = new BookRepository().GetBooks(); var cheapBooks = books.FindAll(b => b.Price < 10); foreach (var book in cheapBooks) { Console.WriteLine(book.Title); } }
static void Main(string[] args) { //args => expression //()=>... // x => ... //(x, y, z)=>... #region 1 //Func<int, int> kareFunc = number => number * number; //Console.WriteLine(kareFunc(2)); #endregion #region 2 const int factor = 14; Func <int, int> carpan = n => n * factor; var sonuc = carpan(10); Console.WriteLine(sonuc); #endregion var books = new BookRepository().GetBooks(); var lambdaBooks = books.FindAll(b => b.Price < 50); foreach (var book in lambdaBooks) { Console.WriteLine(book.Name); } }
static void Main(string[] args) { // args => expression //number => number * number; // () => ... // x => ... // (x, y, z) => ... // Example 1 //Func<int, int> square = number => number * number; //Console.WriteLine(square(5)); // Example 2 //const int factor = 5; //Func<int, int> multiplar = n => n * factor; //var result = multiplar(10); //Console.WriteLine(result); // Example 3 var books = new BookRepository().GetBooks(); // Use predicate //var cheaperBooks1 = books.FindAll(IsCheaperThan10Dollars); // Use lambda var cheaperBooks2 = books.FindAll(book => book.Price < 10); foreach (var book in cheaperBooks2) { Console.WriteLine(book.Title); } }
static void Main(string[] args) { // args => expressions // ()=> ... for no arguments // one argument no parenthesis needed and if multiple arguments parenthesis is needed. // Implementing Lambda Expressions Using Func<> Delegate // 1st int is the input Parameter and 2nd input is the output Parameter Type Func <int, int> cs = calcSquare; // Pointing to a function Func <int, int> cs2 = num => num * num; Console.WriteLine("USING DEFINED FUNTION" + calcSquare(5)); Console.WriteLine("USING FUNC REF TO A FUNCTION: " + cs(10)); Console.WriteLine("USING FUNC REF LAMBDA EXPRESSION: " + cs2(20)); //Finding Books Based on Price Value using Lambda Expressions var books = new BookRepository().GetBooks(); var expensiveBooks = books.FindAll(b => b.Price > 100); // Computes the books that has price greater than 100 in the Book Repository foreach (var price in expensiveBooks) { Console.WriteLine("Expensive Books in List are : {0}", price.Title); // Prints the book/books that are expensive than 100 } Console.ReadLine(); }
static void Main(string[] args) { //Func<int, int> square = number => number * number; Func <int, int> square = number => number * number; square += Multiply; square += MultiplyByThree; square += Multiply; Action writeSomething = WriteSomething; Console.WriteLine(square(413)); Console.WriteLine(writeSomething); var books = new BookRepository().GetBooks(); var cheapBooks = books.FindAll(book => book.Price < 10); //foreach (var book in cheapBooks) //{ // Console.WriteLine(book.Title); //} }
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(); }
static void Main(string[] args) { // args => expression // () => ... // x => ... // (x, y, z) => ... Func <int, int> square = number => number * number; Console.WriteLine(square(5)); // 25 const int factor = 5; Func <int, int> multiplier = n => n * factor; var result = multiplier(10); Console.WriteLine(result); // 50 var books = new BookRepository().GetBooks(); // var cheapBooks = books.FindAll(IsCheaperThan10Dollars); var cheapBooks = books.FindAll(book => book.Price < 10); foreach (var book in cheapBooks) { Console.WriteLine(book.Title); } }
static void Main(string[] args) { // args => expression // numbner => numbner * numbner // () => ... // x => ... // (x, y, z) => ... // Func<int, int> square = number => number * number; // int Square(int number) => number * number; // Console.WriteLine(square(5)); // // const int factor = 5; // int multipier(int n) => n * factor; // Func<int, int> Multipier = n => n * factor; // // var result = Multipier(10); // Console.WriteLine(result); //************* var books = new BookRepository().GetBooks(); // var cheapBooks = books.FindAll(IsCheaperThanTenDoalrs); var cheapBooks = books.FindAll(book => book.Price < 10); foreach (var VARIABLE in cheapBooks) { Console.WriteLine(VARIABLE.Title); } }
static void Main(string[] args) { // args => expression (args goes to expression) //number => number * number; // if we dont have any arguments, need to write it like this: // () => ..codehere.. // x => ... //for multiple arguments: // (x, y, z) => ... Func <int, int> squareLambda = number => number * number; Console.WriteLine(squareLambda(5)); Console.WriteLine(SquareFunction(5)); const int factor = 5; Func <int, int> multiplier = n => n * factor; Console.WriteLine(multiplier(2)); //more real life: var books = new BookRepository().GetBooks(); //var cheapBooks = books.FindAll(IsCheaperThan10Dollars); //a predicate is a delegate that points to a method (in this case it takes type Book) //you can instead use a lambda and just insert it straight in: var cheapBooks = books.FindAll(b => b.Price < 10); //common to use single letter in lambdas foreach (var book in cheapBooks) { Console.WriteLine(book.Title); } }
static void Main(string[] args) { // args => expression //number => number * number; // () => ... // x => ... // (x, y, z) => ... //Func<int, int> square = number => number*number; //Console.WriteLine(square(5)); //const int factor = 5; //Func<int, int> multiplier = n => n * factor; //can use variables defined in the class and the ones passed through the function. //var result = multiplier(10); //Console.WriteLine(result); var books = new BookRepository().GetBooks(); //var cheapBooks = books.FindAll(IsCheaperThanTenDollars); var cheapBooks = books.FindAll(b => b.Price < 10); foreach (var book in cheapBooks) { Console.WriteLine(book.Title); } }
static void Main(string[] args) { #region lambda basics // lambda expression: args => expression // () => ... // x => ... // (x, y, y) => ... //const int factor = 5; //Func<int, int> multiplier = (x) => x * factor; //Console.WriteLine(multiplier(5)); #endregion #region works with list of books var books = new BookRepository().GetBooks(); var findBooks = books.FindAll(b => b.Price > 5); foreach (var book in findBooks) { Console.WriteLine($"{book.Title} - {book.Price}"); } #endregion }
static void Main(string[] args) { // args => expression // () => ... // x => ... // x, y, z => ... Func <int, int> square = Square; Func <int, int> squareInLine = number => number * number; const int factor = 5; Func <int, int> multiplier = number => number * factor; var result = multiplier(5); Console.WriteLine("Square " + square(5)); Console.WriteLine("Square in line " + squareInLine(5)); Console.WriteLine("Multiplier " + result); // Using in predicates (FindAll) var books = new BookRepository().GetBooks(); //var cheapBooks = books.FindAll(IsCheaperThan10Dollars); var cheapBooks = books.FindAll(b => b.Price < 10); foreach (var book in cheapBooks) { Console.WriteLine(book.Title); } }
static void Main(string[] args) { #region One Func <int, int> square = number => number * number; Console.WriteLine(square(5)); const int factor = 5; Func <int, int> multiplier = n => n * factor; Console.WriteLine(multiplier(10)); #endregion #region Two var books = new BookRepository().GetBooks(); var cheapBooks = books.FindAll(b => b.Price < 10); foreach (var book in cheapBooks) { Console.WriteLine(book.Title); } #endregion Console.ReadLine(); }
static void Main(string[] args) { var books = new BookRepository().GetBooks(); var cheaperBooks = books.FindAll(x => x.Price > 10); // Using LE foreach (var book in cheaperBooks) { Console.WriteLine(book.Title); } }
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); } }
private static void Main(string[] args) { //Func<int, int> square = number => number * number; //Console.WriteLine(square(5)); var books = new BookRepository().GetBooks(); var cheapBooks = books.FindAll(a => a.Price < 300); foreach (var book in cheapBooks) { Console.WriteLine(book.Title); } }
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); } }
static void SecondExample() { var books = new BookRepository().GetBooks(); var cheapBooks = books.FindAll(IsCheaperThan10Dolllars); foreach (var book in cheapBooks) { Console.WriteLine(book.Title); } /* What is a predicate ? * - a predicate is basically a delegate which points to a method that gets a book(in this case) * and returns a boolean value specifying if a given condition was satisfied */ var cheapBooksLambdaExpression = books.FindAll(b => b.Price < 10); foreach (var b in cheapBooks) { Console.WriteLine(b.Title); } }
//static void Main(string[] args) //{ // var books = new BookRepository().GetBooks(); //books is a ist of Book, so we can use methods of class List, for example FindAll() -it requires a predicate as an argument // var cheapBooks = books.FindAll(IsCheaperThan10); //cheapBooks is a list of Book // foreach (var item in cheapBooks) // { // Console.WriteLine(item.Title + " " + item.Price); // } // Console.ReadKey(); //} //static bool IsCheaperThan10(Book book) // !!! it's a predicate - a method, that returns true or false, dependig in condition inside //{ // return book.Price < 10; //} static void Main(string[] args) { var books = new BookRepository().GetBooks(); //books is a ist of Book, so we can use methods of class List, for example FindAll() -it requires a predicate as an argument var cheapBooks = books.FindAll(b => b.Price < 10); foreach (var item in cheapBooks) { Console.WriteLine(item.Title + " " + item.Price); } Console.ReadKey(); }
static void Main(string[] args) { var basic = new Basics(); basic.Example(); var books = new BookRepository().GetBooks(); //FindAll is a method that uses a predicate which return bool value. var cheapbooks = books.FindAll(IsCheaperThan50Dollar); //the code inside this method will iterate the collection and will return the elemnts which match the condition of predicate foreach (var book in cheapbooks) { Console.WriteLine(book.Title); } //now the same using lamdaexp: var cheapbk = books.FindAll(bk => bk.Price < 50); foreach (var book in cheapbk) { Console.WriteLine(book.Title); } }
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); } //for finding square Func <int, int> square = s => s * s; Console.WriteLine(square(5)); }
static void Main(string[] args) { // LAMBDA EXPRESS İLE // args => expression //number => number * number; // Func<> örnek olarak built-in bir delegate tir.. //Action<> da bir built-int delegate tir. Func <int, int> square = Square; //veya Func <int, int> square1 = number => number * number; Console.WriteLine(Square(5)); //25 Console.WriteLine(square(5)); //25 Console.WriteLine(square1(5)); //25 var books = new BookRepository().GetBooks(); var cheapBooks = books.FindAll(IsCheaperThan10Dollars); // daha da güzeli, lambda expression ile var cheapBooks1 = books.FindAll(book => book.Price < 10); // c# dilinde bu tarz şeylerde tek harf kullanmayı da çok severlermiş, altta: var cheapBooks2 = books.FindAll(b => b.Price < 10); }
static void Main(string[] args) { // NumbersExample(); var books = new BookRepository().GetBooks(); //var cheapBooks = books.FindAll(isCheaperThan10); ** Using the delegate option var cheapBooks = books.FindAll(b => b.Price < 10); foreach (var book in cheapBooks) { Console.WriteLine(book.Title); } }
static void Main(string[] args) { //arguments => expresion //No arguments, () => ... //one arguments we can avoid using parenthesis, x => ... //More than one arguments we have to use parenthesis, (x,y) => ... Func <int, int> square = number => number * number; Console.WriteLine(square(5)); //Scope const int factor = 5; Func <int, int> multipler = n => n * factor; int result = multipler(10); //Way 1 - Using Predicate functions var books = new BookRepository().GetBooks(); var cheapBooks1 = books.FindAll(IsCheaperThan10); foreach (var book in cheapBooks1) { Console.WriteLine(book.Title); } //Way 2 - Using lambda expression and LINQ BookRepository bookRepository = new BookRepository(); List <Book> cheapBooks2 = (bookRepository.GetBooks()).Where(book => book.Price < 10).ToList(); Console.WriteLine("--------------------"); foreach (var book in cheapBooks2) { Console.WriteLine(book.Title); } //Way 3 - Using Lambda expression var books3 = new BookRepository().GetBooks(); var cheapBooks3 = books3.FindAll(b => b.Price < 10); Console.WriteLine("--------------------"); foreach (var book in cheapBooks3) { Console.WriteLine(book.Title); } }
static void Main(string[] args) { var books = new BookRepository().GetBooks(); //var cheapBooks = books.FindAll(IsCheaperThan10Dollars); //foreach (var book in cheapBooks) // Console.WriteLine(book.Title); // Lambda expression solution var cheapBooks = books.FindAll(b => b.Price < 10); foreach (var book in cheapBooks) { Console.WriteLine(book.Title); } }
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)); }