23. Extension Methods and Linq - C# Fundamentals

38,136 views
40,756 views

Published on

This presentation cover the following topics:
How to define and using Extension methods? What are Anonymouse types?
What is Lamda expression and how to use it?
How to use the LINQ Queries in C#?
Telerik Software Academy: http://www.academy.telerik.com
The website and all video materials are in Bulgarian.

Extension methods
Anonymous types
Lambda expressions
LINQ Query keywords

C# Programming Fundamentals Course @ Telerik Academy
http://academy.telerik.com

Published in: Education, Technology
0 Comments
14 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
38,136
On SlideShare
0
From Embeds
0
Number of Embeds
27,534
Actions
Shares
0
Downloads
458
Comments
0
Likes
14
Embeds 0
No embeds

No notes for slide

23. Extension Methods and Linq - C# Fundamentals

  1. 1. Extension Methods and LINQ Extension Methods, Anonymous Types LINQ Query Keywords, Lambda ExpressionsSvetlin NakovManager Technical Trainerhttp://www.nakov.com/Telerik Software Academyhttp://academy.telerik.com/
  2. 2. Contents1. Extension methods2. Anonymous types3. Lambda expressions4. LINQ Query keywords 2
  3. 3. Extension Methods
  4. 4. Extension Methods Once a type is defined and compiled into an assembly its definition is, more or less, final  The only way to update, remove or add new members is to recode and recompile the code Extension methods allow existing compiled types to gain new functionality  Without recompilation  Without touching the original assembly 4
  5. 5. Defining Extension Methods Extension methods  Defined in a static class  Defined as static  Use this keyword before its first argument to specify the class to be extended Extension methods are "attached" to the extended class  Can also be called from statically through the defining static class 5
  6. 6. Extension Methods – Examplespublic static class Extensions{ public static int WordCount(this string str) { return str.Split(new char[] { , ., ? }, StringSplitOptions.RemoveEmptyEntries).Length; }}...static void Main(){ string s = "Hello Extension Methods"; int i = s.WordCount(); Console.WriteLine(i);} 6
  7. 7. Extension Methods – Examples (2)public static void IncreaseWidth( this IList<int> list, int amount){ for (int i = 0; i < list.Count; i++) { list[i] += amount; }}...static void Main(){ List<int> ints = new List<int> { 1, 2, 3, 4, 5 }; ints.IncreaseWidth(5); // 6, 7, 8, 9, 10} 7
  8. 8. Extension Methods Live Demo
  9. 9. Anonymous Types
  10. 10. Anonymous Types Anonymous types  Encapsulate a set of read-only properties and their value into a single object  No need to explicitly define a type first To define an anonymous type  Use of the new var keyword in conjunction with the object initialization syntax var point = new { X = 3, Y = 5 }; 10
  11. 11. Anonymous Types – Example // Use an anonymous type representing a car var myCar = new { Color = "Red", Brand = "BMW", Speed = 180 }; Console.WriteLine("My car is a {0} {1}.", myCar.Color, myCar.Brand); At compile time, the C# compiler will autogenerate an uniquely named class The class name is not visible from C#  Using implicit typing (var keyword) is mandatory 11
  12. 12. Anonymous Types – Properties Anonymous types are reference types directly derived from System.Object Have overridden version of Equals(), GetHashCode(), and ToString()  Do not have == and != operators overloaded var p = new { X = 3, Y = 5 }; var q = new { X = 3, Y = 5 }; Console.WriteLine(p == q); // false Console.WriteLine(p.Equals(q)); // true 12
  13. 13. Arrays of Anonymous Types You can define and use arraysof anonymous types through the following syntax: var arr = new[] { new { X = 3, Y = 5 }, new { X = 1, Y = 2 }, new { X = 0, Y = 7 } }; foreach (var item in arr) { Console.WriteLine("({0}, {1})", item.X, item.Y); } 13
  14. 14. Anonymous Types Live Demo
  15. 15. LINQ and Query Keywords
  16. 16. Lambda Expressions
  17. 17. Lambda Expressions A lambda expression is an anonymous function containing expressions and statements  Used to create delegates or expression tree types All lambda expressions use the lambda operator =>, which is read as "goes to"  The left side of the lambda operator specifies the input parameters  The right side holds the expression or statement 17
  18. 18. Lambda Expressions – Examples Usually used with collection extension methods like FindAll() and RemoveAll() List<int> list = new List<int>() { 1, 2, 3, 4 }; List<int> evenNumbers = list.FindAll(x => (x % 2) == 0); foreach (var num in evenNumbers) { Console.Write("{0} ", num); } Console.WriteLine(); // 2 4 list.RemoveAll(x => x > 3); // 1 2 3 18
  19. 19. Sorting with Lambda Expressionvar pets = new Pet[]{ new Pet { Name="Sharo", Age=8 }, new Pet { Name="Rex", Age=4 }, new Pet { Name="Strela", Age=1 }, new Pet { Name="Bora", Age=3 }};var sortedPets = pets.OrderBy(pet => pet.Age);foreach (Pet pet in sortedPets){ Console.WriteLine("{0} -> {1}", pet.Name, pet.Age);} 19
  20. 20. Lambda Code Expressions Lambda code expressions: List<int> list = new List<int>() { 20, 1, 4, 8, 9, 44 }; // Process each argument with code statements List<int> evenNumbers = list.FindAll((i) => { Console.WriteLine("value of i is: {0}", i); return (i % 2) == 0; }); Console.WriteLine("Here are your even numbers:"); foreach (int even in evenNumbers) Console.Write("{0}t", even); 20
  21. 21. Delegates Holding Lambda Functions Lambda functions can be stored in variables of type delegate  Delegates are typed references to functions Standard function delegates in .NET:  Func<TResult>, Func<T, TResult>, Func<T1, T2, TResult>, … Func<bool> boolFunc = () => true; Func<int, bool> intFunc = (x) => x < 10; if (boolFunc() && intFunc(5)) Console.WriteLine("5 < 10"); 21
  22. 22. Lambda Expressions Live Demo
  23. 23. LINQ and Query Keywords Language Integrated Query (LINQ) query keywords  from – specifies data source and range variable  where – filters source elements  select – specifies the type and shape that the elements in the returned sequence  group – groups query results according to a specified key value  orderby – sorts query results in ascending or descending order 23
  24. 24. Query Keywords – Examples select, from and where clauses: int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var querySmallNums = from num in numbers where num < 5 select num; foreach (var num in querySmallNums) { Console.Write(num.ToString() + " "); } // The result is 4 1 3 2 0 24
  25. 25. Query Keywords – Examples (2) Nested queries: string[] towns = { "Sofia", "Varna", "Pleven", "Ruse", "Bourgas" }; var townPairs = from t1 in towns from t2 in towns select new { T1 = t1, T2 = t2 }; foreach (var townPair in townPairs) { Console.WriteLine("({0}, {1})", townPair.T1, townPair.T2); } 25
  26. 26. Query Keywords – Examples (3) Sorting with оrderby: string[] fruits = { "cherry", "apple", "blueberry", "banana" }; // Sort in ascending sort var fruitsAscending = from fruit in fruits orderby fruit select fruit; foreach (string fruit in fruitsAscending) { Console.WriteLine(fruit); } 26
  27. 27. LINQ Query Keywords Live Demo
  28. 28. Extension Methods and LINQ курсове и уроци по програмиране, уеб дизайн – безплатно BG Coder - онлайн състезателна система - online judge курсове и уроци по програмиране – Телерик академия форум програмиране, форум уеб дизайн уроци по програмиране и уеб дизайн за ученици ASP.NET курс - уеб програмиране, бази данни, C#, .NET, ASP.NET http://academy.telerik.com/ програмиране за деца – безплатни курсове и уроци ASP.NET MVC курс – HTML, SQL, C#, .NET, ASP.NET MVC безплатен SEO курс - оптимизация за търсачки алго академия – състезателно програмиране, състезаниякурсове и уроци по програмиране, книги – безплатно от Наков курс мобилни приложения с iPhone, Android, WP7, PhoneGap уроци по уеб дизайн, HTML, CSS, JavaScript, Photoshop Дончо Минков - сайт за програмиране free C# book, безплатна книга C#, книга Java, книга C# Николай Костов - блог за програмиране безплатен курс "Качествен програмен код" безплатен курс "Разработка на софтуер в cloud среда" C# курс, програмиране, безплатно
  29. 29. Exercises1. Implement an extension method Substring(int index, int length) for the class StringBuilder that returns new StringBuilder and has the same functionality as Substring in the class String.2. Implement a set of extension methods for IEnumerable<T> that implement the following group functions: sum, product, min, max, average.3. Write a method that from a given array of students finds all students whose first name is before its last name alphabetically. Use LINQ query operators.4. Write a LINQ query that finds the first name and last name of all students with age between 18 and 24. 29
  30. 30. Exercises (2)5. Using the extension methods OrderBy() and ThenBy() with lambda expressions sort the students by first name and last name in descending order. Rewrite the same with LINQ.6. Write a program that prints from given array of integers all numbers that are divisible by 7 and 3. Use the built-in extension methods and lambda expressions. Rewrite the same with LINQ.7. Write extension method to the String class that capitalizes the first letter of each word. Use the method TextInfo.ToTitleCase(). 30
  31. 31. Free Trainings @ Telerik Academy Fundamentals of C# Programming Course  csharpfundamentals.telerik.com Telerik Software Academy  academy.telerik.com Telerik Academy @ Facebook  facebook.com/TelerikAcademy Telerik Software Academy Forums  forums.academy.telerik.com

×