How to explain a delegate in an interview

I know that we have many questions of this type in Stackoverflow, I also know what a delegate is and how it works, but sometimes it is difficult for him to explain the delegate in an interview, any suggestion on how to explain it?

+4
source share
5 answers

This is a strongly typed function pointer, essentially. This is a way to pass arbitrary code as a parameter to a method.

+5
source

A delegate is a C # language element that allows you to reference a method.

+2
source

Some of the ways I heard describe delegates:

  • Language and frame support for first-class features.
  • Interfaces with one method.
  • Pointers to type functions.
  • Objects representing a sequence of operations with a well-defined contract.

If you try in more detail, I would:

  • Distinguish between System.Delegate, delegate types, delegate type variables, and delegation instances; spell out your declaration, purpose, transfer, and challenge.
  • Emphasize their invariable nature of the reference type.
  • Discuss the multicast delegates.
  • List some common types of delegates in the structure, especially the Func <> and Action <> families.
  • Associates delegates with method groups, anonymous methods, lambda expressions, and closures. Explain the use of the delegate keyword in C #, as well as various courtesies provided to the programmer by the compiler
  • Discuss their relationship to streams, events, expression trees, and LINQ.
  • Discussion:
    • Functions of a higher order - currying, combinators, etc.
    • How delegates can be used with reflection.
    • Deviation in relation to delegates.
+2
source

How to use a real living analogy?

http://www.wordreference.com/definition/delegate

trust (task or responsibility) to another person (in this case it would be a method).

In a computational word, you can explain it in the same way. When you pass a delegate to a method, you don’t know about the implementation of the method, you simply reference this method, and the delegate will take care of its implementation.

+1
source

This is an object that encapsulates a method signature and provides an object-oriented mechanism for processing functions as objects without using function pointers.

0
source

Source: https://habr.com/ru/post/1334756/


All Articles