Method signatures of delegate and interface in C#
I ever write an article about the event handling of Java and C#, just want to add some of C#’s delegate.
Delegate has no semantics carried, so does interface, it only provide method signatures.
Let’s look at the following example:
- class Person
- {
- public interface Runnable
- {
- void run();
- }
- public void execute(Person.Runnable runnable)
- {
- }
- }
- class Gym {
- public interface Runnable
- {
- void run();
- }
- }
Class Person’s execute method will not accept the instance that implements the Gym’s Runnable interface.
But in this case,
- class Person
- {
- public delegate void Runnable();
- public void execute(Person.Runnable runnable)
- {
- }
- }
- class Gym {
- public delegate void Runnable();
- }
It’s no problem for Person’s execute method to accept the Gym’s Runnable delegate.
So, delegate defines a single method signature to be wrapped by a delegate instance, but interface defines a set of method signatures to be implemented by a class.
More entries about : Java, C#, Delegate, Interface
Maybe you are also interested in the following entries:
Can I use "==" to compare two String?