This document discusses advanced C# features including delegates, events, anonymous methods, lambda expressions, anonymous types, dynamic types, and extension methods. Delegates allow type-safe callbacks and are used to handle events. Anonymous methods and lambda expressions provide concise ways to handle events without standalone methods. Anonymous types define encapsulated data without associated methods, and are typically used with LINQ. Dynamic types have members that are resolved at runtime rather than compile time, without static type checking.
3. 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
4. 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
5. 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
8. 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 SampleDelegatecan point only to methods that take two integers and return an integer
8
19. 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
21. 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
25. 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
27. Anonymous Types
All anonymous types are automatically derived from System.Objectand, 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
29. 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
30. Dynamic Types
You now have three ways to define data whose underlying type is not directly indicated in our code base
30