Upload
nguyen-patrick
View
48
Download
0
Tags:
Embed Size (px)
DESCRIPTION
Day02 01 Advance Feature in C# DH TDT
Citation preview
Advanced features in C#
1
Agenda
Delegates & Events
Anonymous Method
Lambda Expressions
Anonymous Types & Dynamic Type
Extension Methods
Reflection
Attributes
Q&A
2
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
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
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
Delegates
6
Delegates
7
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
Delegates
9
Delegates
10
Delegates
11
Delegates
12
Delegates
13
Events
14
15
Agenda
Delegates & Events
Anonymous Method
Lambda Expressions
Anonymous Types & Dynamic Type
Extension Methods
Reflection
Attributes
Q&A
16
Anonymous Method
17
Anonymous Method
18
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
Agenda
Delegates & Events
Anonymous Method
Lambda Expressions
Anonymous Types & Dynamic Type
Extension Methods
Reflection
Attributes
Q&A
20
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
Lambda Expressions
22
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
Agenda
Delegates & Events
Anonymous Method
Lambda Expressions
Anonymous Types & Dynamic Type
Extension Methods
Reflection
Attributes
Q&A
24
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
Anonymous Types
26
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
Anonymous Types
Anonymous Types Containing Anonymous Types
28
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
Dynamic Types
You now have three ways to define data whose
underlying type is not directly indicated in our code base
30
Dynamic Types
31
Dynamic Types
Calling Members on Dynamically Declared Data
32
Dynamic Types
You will notsee the expected Visual Studio IntelliSense
33
Dynamic Types
Comparison between three ways to define data
34