Presentation is loading. Please wait.

Presentation is loading. Please wait.

2.3 Cool features in C# academy.zariba.com 1. Lecture Content 1.Extension Methods 2.Anonymous Types 3.Delegates 4.Action and Func 5.Events 6.Lambda Expressions.

Similar presentations


Presentation on theme: "2.3 Cool features in C# academy.zariba.com 1. Lecture Content 1.Extension Methods 2.Anonymous Types 3.Delegates 4.Action and Func 5.Events 6.Lambda Expressions."— Presentation transcript:

1 2.3 Cool features in C# academy.zariba.com 1

2 Lecture Content 1.Extension Methods 2.Anonymous Types 3.Delegates 4.Action and Func 5.Events 6.Lambda Expressions 7.LINQ 2

3 1. Extension Methods 3 Once a class is defined and compiled into an assembly its definition is final – the only way to update the code is to recode and recompile Extension methods allow existing compiled types to gain new functionality without recompilation or changing the original assembly

4 2. Anonymous Types 4 Anonymous types are used to encapsulate a set of read-only properties into a single object Used if there is no need for a separate class The data is going to be used once and then discarded or never used again

5 3. Delegates 5 Delegates are special.NET types that hold a method reference They describe the signature of a given method Their values are methods Delegates are reference types They are roughly similar to function pointers in other languages Can point to a sequence of multiple methods

6 4. Actions and Func 6 Action and Func are simplified delegates Action only defines a delegate with return type void with a given set of parameters – T1,T2,T3... Func only defines a delegate with return type(K) and a given set of parameters (T1,T2,T3) Predicate is a Func which returns a boolean

7 5. Events 7 An Event is a message sent by an object to signal the occurrence of an action Enable a class or object to notify other classes or objects when something interesting occurs Publisher (the class that sends) raises the event Subscribers (the class that receives) handle the event

8 6. Lambda Expressions 8 A lambda expression is an anonymous function It uses the lambda operator => (reads as “go to”) The LHS specifies the parameters The RHS specifies the expression or statement

9 7. LINQ 9 LINQ is a set of extensions to.NET Framework Keywords: From – specifies data source and range of variables Where – filters the source of elements Select – specifies the type and shape that the elements are returned in Group – groups results according to a key value OrderBy – sorts in ascending or descending order

10 7. LINQ with Lambda 10 Keywords: Where – searches by a given condition First/FirstOrDefault – gets the first matched element Last/LastOrDefaut – guess what that does Select/Cast – makes a projection/conversion to another type OrderBy/ThenBy/+Descending – sorts a collection Any – checks if any element matches a condition All – checks if all elements match a condition ToArray/ToList/AsEnumerable Reverse

11 7. LINQ with Lambda 11 Keywords: Average – calculates the average Count – counts the elements Max – Determines the maximum value Sum – sums the values in a collection

12 Homework 12 1. Implement an extension method Substring(int index, int length) for the StringBuilder. Test your application. 2. Implement a set of extension method for IEnumerable - sum, product, min, max, average 3. Create a class Student with properties FirstName, LastName, Age, Faculty number, Phone, email, marks (a list) and group number. Create a sample list of students. Do the below exercises with the list. 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. Write a LINQ query that finds the first and last name of all students with age between 18 and 24. Sort a list of students by first name and then by last name using lambda. Select all students from group 2 and sort them by first name. Extract all students who have emails in abv.bg Extract all students with phones in Sofia Extract students that have at least one mark Excellent 6 into a new anonymous class that has properties FullName and Marks Extract all students who have exactly two marks “2”.

13 Homework 13 4. Given an array of numbers, list all numbers that are divisible by 7 and 3. Use lambda. 5. Using delegates write a class Timer that can execute certain method at each t seconds. 6. Rewrite the above class using events. 7. Write a program to return the string with maximum length from an array of strings.

14 14 References

15 15 Zariba Academy Questions


Download ppt "2.3 Cool features in C# academy.zariba.com 1. Lecture Content 1.Extension Methods 2.Anonymous Types 3.Delegates 4.Action and Func 5.Events 6.Lambda Expressions."

Similar presentations


Ads by Google