Sorry..but really could not complete it on time. Also there are quite a few things that you may still require to read from other sources. Personally I found it difficult to complete this chapter.
Delegates- In essence, a delegate is a type-safe object that points to another method (or possibly a list of methods). E.g. This delegate can point to any method taking two integers and returning an integer.
public delegate int BinaryOp(int x, int y);
When the C# compiler processes delegate types, it automatically generates a sealed class deriving from System.MulticastDelegate. For the above example C# compiler will generate following code in bold are the items specified in declaration-
sealed class BinaryOp : System.MulticastDelegate
{
public BinaryOp(object target, uint functionAddress);
public int Invoke(int x, int y);
public IAsyncResult BeginInvoke(int x, int y,
AsyncCallback cb, object state);
public int EndInvoke(IAsyncResult result);
}
BeginInvoke() and EndInvoke() provide the ability to call the current method asynchronously on a separate thread of execution. Invoke() is perhaps the core method, as it is used to invoke each method maintained by the delegate type in a synchronous manner, meaning the caller must wait for the call to complete before continuing on its way. Further Multicast delegate derives from Delegate class which implements ICloneable and ISerializable. Also, it is not possible to directly derive from these classes.
A simple delegate example will be explained in the code you can also determine 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. Also,
Multicasting - When you wish to add multiple methods to a delegate object, you simply make use of the overloaded += operator, rather than a direct assignment. The Delegate class also defines a static Remove() method that allows a caller to dynamically remove a member from the invocation list.
Delegate Covariance - Given the laws of classic inheritance, it would be ideal to build a single delegate type that can point to methods returning either Car or SportsCar objects (after all, a SportsCar “is-a” Car). Covariance (which also goes by the term relaxed delegates) allows for this very possibility. Simply put, covariance allows you to build a single delegate that can point to methods returning class types related by classical inheritance.
In a similar vein, contravariance allows you to create a single delegate that can point to numerous
methods that receive objects related by classical inheritance. Consult the .NET Framework 3.5 SDK documentation
for further details.
Generic Delegates - We can also have Generic Delegates . For e.g. This generic delegate can call any method returning void and taking a single parameter.
public delegate void MyGenericDelegate(T arg)
Events in ASP.NET - You can declare a event using ‘event’ keyword when compiler processes the event keyword, you are automatically provided with registration and unregistration methods as well as any necessary member variables for your delegate types. These delegate member variables are always declared private, and therefore they are not directly exposed from the object firing the event and hence overcome the problem with delegates. To be sure, the event keyword is little more than syntactic sugar in that it simply saves you some typing time. Defining an event is a two-step process. First, you need to define a delegate that will hold the list of methods to be called when the event is fired. Next, you declare an event (using the C# event keyword) in terms of the related delegate. A C# event actually expands into two hidden public methods, one having an add_ prefix, the other having a remove_ prefix.
C# Anonymous Methods - It is possible to associate a delegate directly to a block of code statements at the time of event registration. Formally, such code is termed an anonymous method. You must terminate the method with a semicolon. Anonymous methods are interesting in that they are able to access the local variables of the method that defines them. Formally speaking, such variables are termed outer variables of the anonymous method.
C# 2008 Lambda Operator - Lambda expressions are nothing more than a more concise way to author anonymous methods and ultimately simplify how we work with the .NET delegate type. You can use the operator ‘=>’ and declare anonymous method after it.

0 comments:
Post a Comment