Refer below code to understand :
using System;
delegate void DelMethod(int x); // This is the delegate declaration
// Example is Class containing static method and instance method to be instantiated
class Example
{
public static void StaticMethod(int i) {
Console.WriteLine("M1: " + i);
}
public void InstanceMethod(int i) {
Console.WriteLine("M3: " + i);
}
}
// Below class contains code to instantiate and invoke static and instance method using delegates
class Test
{
static void Main() {
DelMethod cd1 = new DelMethod (Example.StaticMethod); // Instantiate Static Method delegate
cd1(-1); // Invoke Static Method
Example c = new Example ();
DelMethod cd2 = new DelMethod (c.InstanceMethod); // Instantiate InstanceMethod delegate
cd2(5); // Invoke Instance Method
}
}
Subscribe to:
Post Comments (Atom)
Popular Posts
-
Cache Callback provides an ability where by some actions can be performed when that item is removed from cache CacheItemRemovedCallback ex...
-
XML DOM loads the entire xml document into memory whereas Simple API for XML ( SAX ) does not load entire xml document into memory XML DOM ...
-
Different types of diagrams in UML : UseCase diagram Class diagram Object diagram State diagram Sequence diagram Collaboration diagram...
-
Virtual-Override and New Class Child inherits from Class Parent and has mehtod ShowVirtualOverride() and ShowNew() methods. Please see th...
-
Question : Name few C# 3.0 enhancements ? Answer : The C# 3.0 language enhancements are as follows: Implicitly typed local variables I...
-
If locaction attribute is specified, must be one of: Any, Client, Downstream, None, Server or ServerAndClient.
No comments:
Post a Comment