1.Delegates can only be methods. Here is an example:
delegate void sampleDelegate();
Interface can include both properties and methods.
Here is an example for an interface:
interface ItestInterface
{
int paraml {
get; set; }
void sampleMethod();
}
2. Delegate can be applied to only one method at a time When a class implements an interface, it can implement all the methods associated with it
3. You can use a delegate that is visible in your scope
You can use an interface only when your class or struct implements it
4. Within a class, you can implement the same delegate any number of times. Assume that either sampleClass1 or sampleClass2 of Examplel includes a method called sampleMethod2( ) with the same signature as that of delegate, then the same delegate can be used to access both sampleMethod() as well as sampleMethod2( )
Within a class, you can implement an interface method only once. In Example2, interface ITestInterface has a method called sampleMethod(). When sampleClass1 implements ITestInterface it implements sampleMethod() only once. If not, then it will end up in error.
5. Delegate can implement any method that shares the same signature as that of the delegate When an interface method is implemented, same method name and signature has to be overridden.
6. Delegate is mainly used for handling events
Interfaces are not used for handling events
7. You need not bother about the other methods available in the class.You are concerned about only the method that matches delegate signature.
When a class implements an interface, though the class requires only one method it has to implement all the methods of the interface
8. To access a method using delegate, you need not require any access to the instance of the class where the method is defined
To access the method, you need an instance of the class which implements the interface or you need an interface reference pointing to the method implemented by the class
9.You can access anonymous methods using delegates
You cannot access anonymous methods.Only named methods declared in interface can be accessed by the implementing class.
10.When you call a method using a delegate, all the method pointers associated with the delegate will be scanned through before the method execution. This is not a direct method call as you assume. It has a considerable performance overhead.
When you are calling a method using interface reference, you are directly accessing the method of the class that implements the interface. This is a direct method call and it doesn't have any overhead.
11. Delegates can wrap methods of sealed classes.Sealed classes are those which cannot be inherited.Accessing sealed types is not permissible in interface.
12. Delegates can wrap any method matching its signature irrespective of which ever class the method belongs to.
Class can implement any number of interfaces and it should override only the methods belonging to those interfaces
13. Delegates can wrap static methods. Examplel discussed above has used the delegate to wrap a static method called sampleMethod()
This provision is not available with interfaces .
14. Delegate cannot involve in inheritance.
Interface can inherit other interfaces. When a class implements that interface, it has to implement all the methods belonging to the interface and its inherited interfaces as well.
Here is an example of an interface inheriting from other interfaces:
interface IInterface: IInterface,1 IInterface2
{
void sampleMethod1();
void sampleMethod2();
}
delegate void sampleDelegate();
Interface can include both properties and methods.
Here is an example for an interface:
interface ItestInterface
{
int paraml {
get; set; }
void sampleMethod();
}
2. Delegate can be applied to only one method at a time When a class implements an interface, it can implement all the methods associated with it
3. You can use a delegate that is visible in your scope
You can use an interface only when your class or struct implements it
4. Within a class, you can implement the same delegate any number of times. Assume that either sampleClass1 or sampleClass2 of Examplel includes a method called sampleMethod2( ) with the same signature as that of delegate, then the same delegate can be used to access both sampleMethod() as well as sampleMethod2( )
Within a class, you can implement an interface method only once. In Example2, interface ITestInterface has a method called sampleMethod(). When sampleClass1 implements ITestInterface it implements sampleMethod() only once. If not, then it will end up in error.
5. Delegate can implement any method that shares the same signature as that of the delegate When an interface method is implemented, same method name and signature has to be overridden.
6. Delegate is mainly used for handling events
Interfaces are not used for handling events
7. You need not bother about the other methods available in the class.You are concerned about only the method that matches delegate signature.
When a class implements an interface, though the class requires only one method it has to implement all the methods of the interface
8. To access a method using delegate, you need not require any access to the instance of the class where the method is defined
To access the method, you need an instance of the class which implements the interface or you need an interface reference pointing to the method implemented by the class
9.You can access anonymous methods using delegates
You cannot access anonymous methods.Only named methods declared in interface can be accessed by the implementing class.
10.When you call a method using a delegate, all the method pointers associated with the delegate will be scanned through before the method execution. This is not a direct method call as you assume. It has a considerable performance overhead.
When you are calling a method using interface reference, you are directly accessing the method of the class that implements the interface. This is a direct method call and it doesn't have any overhead.
11. Delegates can wrap methods of sealed classes.Sealed classes are those which cannot be inherited.Accessing sealed types is not permissible in interface.
12. Delegates can wrap any method matching its signature irrespective of which ever class the method belongs to.
Class can implement any number of interfaces and it should override only the methods belonging to those interfaces
13. Delegates can wrap static methods. Examplel discussed above has used the delegate to wrap a static method called sampleMethod()
This provision is not available with interfaces .
14. Delegate cannot involve in inheritance.
Interface can inherit other interfaces. When a class implements that interface, it has to implement all the methods belonging to the interface and its inherited interfaces as well.
Here is an example of an interface inheriting from other interfaces:
interface IInterface: IInterface,1 IInterface2
{
void sampleMethod1();
void sampleMethod2();
}
No comments:
Post a Comment