34
Advanced features in C# 1

Day02 01 Advance Feature in C# DH TDT

Embed Size (px)

DESCRIPTION

Day02 01 Advance Feature in C# DH TDT

Citation preview

Page 1: Day02 01 Advance Feature in C# DH TDT

Advanced features in C#

1

Page 2: Day02 01 Advance Feature in C# DH TDT

Agenda

Delegates & Events

Anonymous Method

Lambda Expressions

Anonymous Types & Dynamic Type

Extension Methods

Reflection

Attributes

Q&A

2

Page 3: Day02 01 Advance Feature in C# DH TDT

Delegates

Historically, the Windows API made frequent use of C-

style function pointers to create entities termed callback

functions

Using callbacks, programmers were able to configure

one function to report back to (call back) another function

in the application

With this approach, Windows developers were able to

handle button-clicking, mouse-moving, menu-selecting,

and general bidirectional communications between two

entities in memory

3

Page 4: Day02 01 Advance Feature in C# DH TDT

Delegates

Ideally, you should be able to configure callbacks to

include additional type-safe information such as the

number of (and types of) parameters and the return type

(if any) of the method pointed to

Sadly, this is not the case in traditional callback functions

In the .NET Framework, callbacks are still possible, and

this functionality is accomplished in a much safer and

more object-oriented manner using delegates

4

Page 5: Day02 01 Advance Feature in C# DH TDT

Delegates

In essence, a delegate is a type-safe object that points to

another method (or possibly a list of methods) in the

application, which can be invoked at a later time.

Specifically, a delegate maintains three important pieces

of information

The address of the method on which it makes calls

The parameters(if any) of this method

The return type(if any) of this method

5

Page 6: Day02 01 Advance Feature in C# DH TDT

Delegates

6

Page 7: Day02 01 Advance Feature in C# DH TDT

Delegates

7

Page 8: Day02 01 Advance Feature in C# DH TDT

Delegates

Recall that .NET delegates are type safe. Therefore, if

you attempt to pass a delegate a method that does not

match the pattern, you receive a compile-time error

Given that the SampleDelegate can point only to

methods that take two integers and return an integer

8

Page 9: Day02 01 Advance Feature in C# DH TDT

Delegates

9

Page 10: Day02 01 Advance Feature in C# DH TDT

Delegates

10

Page 11: Day02 01 Advance Feature in C# DH TDT

Delegates

11

Page 12: Day02 01 Advance Feature in C# DH TDT

Delegates

12

Page 13: Day02 01 Advance Feature in C# DH TDT

Delegates

13

Page 14: Day02 01 Advance Feature in C# DH TDT

Events

14

Page 15: Day02 01 Advance Feature in C# DH TDT

15

Page 16: Day02 01 Advance Feature in C# DH TDT

Agenda

Delegates & Events

Anonymous Method

Lambda Expressions

Anonymous Types & Dynamic Type

Extension Methods

Reflection

Attributes

Q&A

16

Page 17: Day02 01 Advance Feature in C# DH TDT

Anonymous Method

17

Page 18: Day02 01 Advance Feature in C# DH TDT

Anonymous Method

18

Page 19: Day02 01 Advance Feature in C# DH TDT

Anonymous Method

Strictly speaking, you are not required to receive the

incoming arguments sent by a specific event.

However, if you want to make use of the possible

incoming arguments, you will need to specify the

parameters prototyped by the delegate type

Accessing Local Variables? (Page 450 – ebook)

19

Page 20: Day02 01 Advance Feature in C# DH TDT

Agenda

Delegates & Events

Anonymous Method

Lambda Expressions

Anonymous Types & Dynamic Type

Extension Methods

Reflection

Attributes

Q&A

20

Page 21: Day02 01 Advance Feature in C# DH TDT

Lambda Expressions

C# supports the ability to handle events “inline” by

assigning a block of code statements directly to an event

using anonymous methods, rather than building a stand-

alone method to be called by the underlying delegate

Lambda expressions are nothing more than a very

concise[ngắn-gọn] way to author anonymous methods and

ultimately simplify how we work with the .NET delegate

type

21

Page 22: Day02 01 Advance Feature in C# DH TDT

Lambda Expressions

22

Page 23: Day02 01 Advance Feature in C# DH TDT

Lambda Expressions

ArgumentsToProcess => StatementsToProcessThem

List<int> evenNumbers =

list.FindAll(i => (i % 2) == 0);

List<int> evenNumbers =

list.FindAll(delegate (int i)

{

return (i % 2) == 0;

});

23

Page 24: Day02 01 Advance Feature in C# DH TDT

Agenda

Delegates & Events

Anonymous Method

Lambda Expressions

Anonymous Types & Dynamic Type

Extension Methods

Reflection

Attributes

Q&A

24

Page 25: Day02 01 Advance Feature in C# DH TDT

Anonymous Types

Whenever you need to define a class that is intended to

be reused across projects and provides numerous bits of

functionality through a set of methods, events,

properties, and custom constructors, creating a new C#

class is common practice

However, there are other times when you would like to

define a class simply to model a set of encapsulated

(and somehow related) data points without any

associated methods, events, or other specialized

functionality

Furthermore, what if this type is to be used only by a

handful of methods in your program?25

Page 26: Day02 01 Advance Feature in C# DH TDT

Anonymous Types

26

Page 27: Day02 01 Advance Feature in C# DH TDT

Anonymous Types

All anonymous types are automatically derived from

System.Object and, therefore, support each of the

members provided by this base class. Given this, we

could invoke ToString(), GetHashCode(), Equals(),or

GetType()

The Semantics of Equality for Anonymous Types?

27

Page 28: Day02 01 Advance Feature in C# DH TDT

Anonymous Types

Anonymous Types Containing Anonymous Types

28

Page 29: Day02 01 Advance Feature in C# DH TDT

Anonymous Types

Anonymous type declarations should be used

sparingly[hạn-chế], typically only when making use of the

LINQ technology

Anonymous types’ numerous limitations:

You don’t control the name of the anonymous type

Anonymous types always extend System.Object

The fields and properties of an anonymous type are always

read-only

cannot support events, custom methods, custom operators, or

custom overrides

Anonymous types are always implicitly sealed.

Anonymous types are always created using the default

constructor. 29

Page 30: Day02 01 Advance Feature in C# DH TDT

Dynamic Types

You now have three ways to define data whose

underlying type is not directly indicated in our code base

30

Page 31: Day02 01 Advance Feature in C# DH TDT

Dynamic Types

31

Page 32: Day02 01 Advance Feature in C# DH TDT

Dynamic Types

Calling Members on Dynamically Declared Data

32

Page 33: Day02 01 Advance Feature in C# DH TDT

Dynamic Types

You will notsee the expected Visual Studio IntelliSense

33

Page 34: Day02 01 Advance Feature in C# DH TDT

Dynamic Types

Comparison between three ways to define data

34