Delegates and Interfaces in C #

I would like to ask this question as long as I try to understand the use and purpose of delegates, although, most likely, they asked it in similar formulations.

I know that delegates serve as pointers to functions that are used in C ++. The fact is that in C # they serve mainly as an alternative to interfaces and polymorphism. Since I can subclass a specific class and provide them with appropriate methods for everyone, what does delegate offer in addition to this? Are there any cases that involve their use, or is it just repairable when using delegates? Would you recommend their widespread deployment across interfaces?

I am talking exclusively about delegates, and I want to distinguish their role from the role of events.

+42
c # interface delegates
Jan 01 '11 at 19:17
source share
5 answers

Yes, delegates are very similar to single-method interfaces. However:

  • CLR has built-in support for them
  • Support within their framework, including multi-sheet capabilities and asynchronous calling
  • Support for additional C # / VB languages ​​in the form of method group transformations, lambda expressions, anonymous methods
  • They have a mandate for events (i.e. events and delegates are a kind of matching pair)
  • They mean that you do not need to implement the interface in a separate class for each delegate instance that you want to create.

The last point is the most important - consider the LINQ expression:

var query = collection.Where(x => x > 5) .Select(x => x * x); 

Now imagine that if you express the logic x > 5 and x * x , you had to write a separate class for each expression and implement the interface: the amount of torsional and useful code would be ridiculous. Now, of course, the language could be designed to allow conversions from lambda expressions to implement the interface through separate classes, but then you still lose the advantage of being able to just write a separate method and create a delegate with that as a target. You would also lose reusable abilities.

As with a similar thought, consider looping statements like while and for . Do we really need them when we have goto ? Nope. But life is much better with them. The same can be said about delegates, as well as properties, events, etc. All of them simplify the development process.

+61
Jan 01 '11 at 19:22
source share

The biggest practical difference is that you can provide different delegate instances for the same delegate from the same class, while you cannot do this with interfaces.

 void delegate XYZ(int p); interface IXyz { void doit(int p); } class One { // All four methods below can be used to implement the XYZ delegate void XYZ1(int p) {...} void XYZ2(int p) {...} void XYZ3(int p) {...} void XYZ4(int p) {...} } class Two : IXyz { public void doit(int p) { // Only this method could be used to call an implementation through an interface } } 
+21
Jan 01 '11 at 19:23
source share

From When to use delegates instead of interfaces (MSDN) :

Both delegates and interfaces allow the class constructor to separate type declarations and implementations. This interface can be inherited and implemented by any class or structure. A delegate can be created for a method in any class if the method matches the method signature for the delegate. A reference to an interface or delegate can be used by an object that does not know a class that implements an interface or delegation method. Given these similarities, when does a class developer use a delegate and when should he use an interface?

Use a delegate in the following cases:

  • The event design pattern is used.
  • It is advisable to encapsulate a static method.
  • The caller does not need to access other properties, methods, or interfaces for an object that implements this method.
  • A light composition is desirable.
  • A class may require more than one method implementation.

Use the interface in the following cases:

  • There is a group of related methods that can be called.
  • A class requires only one method implementation.
  • A class using an interface will want to use this interface for another interface or class types.
  • The method used is associated with the type or class identifier: for example, comparison methods.

One good example of using a single method interface instead of a delegate is IComparable or the generic version of IComparable (Of T). IComparable declares a CompareTo method that returns an integer that indicates less than, equal to, or greater than the relation between two objects of the same type. IComparable can be used as the basis of a sorting algorithm. Although the use of the delegate comparison method as the basis of the sorting algorithm will be valid, it is not ideal. Since the ability to compare belongs to the class, and the comparison algorithm does not change at runtime, an ideal interface with one method.

From Delegates Vs Interfaces to C # :

Delegates and interfaces are two different concepts in C #, but they have in common. Both delegates and interfaces include only the declaration. The implementation is performed using another programming object.

+14
Jan 01 '11 at 19:24
source share

Delegates and interfaces are two different concepts in C #.

Interfaces allow you to expand some of the functionality of the object, this is a contract between the interface and the object that implements it, and delegates are just safe callbacks, they are kind of pointers to functions.

+2
Jan 01 '11 at 7:21
source share

Delegates are the only real benefits over interfaces.

  1. Delegates can deal with different types of arguments even before .Net-supported generics.
  2. A class can create delegates representing several different methods that have the same signature; replacing delegates with interfaces would mean that each class can expose one method that implements each interface in the delegate style without creating helper classes, but a helper class is required for each additional implementation.

    When .net did not support generics, delegates were an integral part of it, since declaring different non-common interfaces for every different function that we would like to transfer would be inoperative. If .net supported generics from the very beginning, delegates would not be necessary, except for certain Reflection-related scripts, and even there, it might be most useful to have the type Action<T,U> - implementation IAction<T,U> ( so there is code that just needs it that Invoke can use the interface). An interface approach would require the creation of classes with one method in cases where classes should create delegates that demonstrate several methods, but eliminated the need to create separate instances of the delegate in many common cases, when the number of methods the given signature to be exposed is exactly one.

    By the way, replacing delegates with interfaces would in no way prevent the creation of a universal Combine method. Indeed, interface covariance can make such a method more efficient than the existing Delegate.Combine in many ways. Implementing a method similar to Delegate.Remove would be awkward and annoying at best, but I can't think of any other situation than managing an event subscription that would require the use of Delegate.Remove , and in any case, it's best to handle a subscription to events using other approaches.

+1
Jan 03 '12 at 18:56
source share



All Articles