A delegate is a .NET class that encapsulates a method, but not in the same way other classes encapsulate methods. A delegate actually stores the address of a method that is contained in some other class. So, a delegate is really the equivalent of a function pointer in C++. However, they are also far more than that.
<from MSDN>
Delegates enable you to call a synchronous method in an asynchronous manner. When you call a delegate synchronously, the
Invoke method calls the target method directly on the current thread. If the
BeginInvoke method is called, the common language runtime (CLR) queues the request and returns immediately to the caller. The target method is called asynchronously on a thread from the thread pool. The original thread, which submitted the request, is free to continue executing in parallel with the target method. If a callback method has been specified in the call to the
BeginInvoke method, the callback method is called when the target method ends. In the callback method, the
EndInvoke method obtains the return value and any input/output or output-only parameters. If no callback method is specified when calling
BeginInvoke,
EndInvoke can be called from the thread that called
BeginInvoke.
Declaring and using delegates
You declare a delegate in a class or namespace using the
delegate keyword and by specifying the signature of the method it will call. The following line declares a delegate named
TestDelegate which will reference a method that returns void and accepts a string as the argument.
delegate void TestDelegate(string s);
Now that we have declared the TestDelegate, we can instantiate it to encapsulate a method of that signature.
Then, we can invoke the method through the delegate, just as if we invoked the method itself.
The next code sample creates an instance of TestDelegate and uses it to invoke the TestFun method.
class DelegateDemo {
TestDelegate td = new TestDelegate(TestFun);
protected void Page_Load(object sender, EventArgs e)
{
td("Hello World");
}
public void TestFun(string s) {
Console.WriteLine(s);
}
}
Delegates only depend on the signature of the method, not on the class or object containing the method.
A delegate can reference an instance method as well. The above example uses the TestDelegate to invoke an
method.
Events
One of the most common uses of delegates in .NET is event handling. Events are useful for notifying objects
of user interface events or state changes. The following example creates a Timer
object that will fire an event
every second. The Timer
class is defined in the System.Timers
namespace.
class DelegateDemo {
static void Main(string[] args) {
Timer t = new Timer(1000);
t.Elapsed +=
new ElapsedEventHandler(Timer_Elapsed);
t.Enabled = true;
Console.WriteLine("Press enter to exit");
Console.ReadLine();
}
static void Timer_Elapsed(object sender, ElapsedEventArgs e) {
Console.WriteLine("tick");
}
}
The Timer
class contains the Elapsed
event and fires it whenever its interval expires. In this example the
Main
method instantiates a Timer
and registers an ElapsedEventHandler
delegate with its Elapsed
event.
In this example, the method invoked by the ElapsedEventHandler
delegate is the Timer_Elapsed
method. Following the convention of all event handling delegates, the ElapsedEventHandler
delegate
returns void
and accepts two parameters. The first is a reference to the object that signaled the event and
the second is a argument derived of EventArgs
which stores pertinent information about the event.
A CROSS APPLY is used when you want to return only those records from your table where a value is returned by your table valued function.
Whereas, an OUTER APPLY is used to return all records from your table regardless of the values returned by your table valued function.
SELECT name.User_Id,name.UserName,name.Email,name.MobileNo,name.VisitedDate
FROM allocate_site lead
outer apply dbo.fn_get_latest_response_userwise(lead.User_ID) sales
cross apply dbo.fn_get_user_details(lead.User_ID) name
inner join UserDetails users on users.ID = lead.User_ID
WHERE lead.ProductId = name.ProductID