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

2.3 Cool features in C# academy.zariba.com - PowerPoint Presentation

pasty-toler
pasty-toler . @pasty-toler
Follow
354 views
Uploaded On 2018-02-22

2.3 Cool features in C# academy.zariba.com - PPT Presentation

1 Lecture Content Extension Methods Anonymous Types Delegates Action and Func Events Lambda Expressions LINQ 2 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 ID: 634267

class students delegates lambda students class lambda delegates linq method types set extension methods anonymous list elements type specifies

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "2.3 Cool features in C# academy.zariba.c..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

2.3 Cool features in C#

academy.zariba.com

1Slide2

Lecture Content

Extension Methods

Anonymous Types

Delegates

Action and

FuncEventsLambda ExpressionsLINQ

2Slide3

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 assemblySlide4

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 againSlide5

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 point

ers

in other languages

Can point to a sequence of multiple methodsSlide6

4. Actions and

Func6

Action

and

Func

are simplified delegates

Action<T1,T2,T3…> only defines a delegate with

return type void

with a given set of parameters – T1,T2,T3...

Func

<T1,T2…,K> only defines a delegate with

return type(K)

and a given set of parameters (T1,T2,T3)

Predicate is a

Func

which returns a

booleanSlide7

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

Subscriber

s (the class that receives)

handle

the eventSlide8

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 statementSlide9

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 orderSlide10

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

ReverseSlide11

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 collectionSlide12

Homework

12

Implement an extension method Substring(

int

index,

int length) for the StringBuilder. Test your application.Implement a set of extension method for IEnumerable<T> - sum, product, min, max, averageCreate 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.

Sor

t 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”.Slide13

Homework

13

Given an array of numbers, list all numbers that are divisible by 7 and 3. Use lambda.

Using delegates write a class Timer that can execute certain method at each t seconds.

Rewrite the above class using events.

Write a program to return the string with maximum length from an array of strings.Slide14

14

ReferencesSlide15

15

Zariba Academy

Questions