Can someone distill into proper English what a delegate is?

前端 未结 11 1062
被撕碎了的回忆
被撕碎了的回忆 2020-12-03 03:27

Can someone please break down what a delegate is into a simple, short and terse explanation that encompasses both the purpose and general benefits? I\'ve tried to wrap my h

11条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-03 03:49

    In the simplest possible terms, it's essentially a pointer to a method.

    You can have a variable that holds a delegate type (just like you would have an int variable that can hold an int type). You can execute the method that the delegate points to by simply calling your variable like a function.

    This allows you to have variable functions just like you might have variable data. Your object can accept delegates from other objects and call them, without having to define all the possible functions itself.

    This comes in very handy when you want an object to do things based on user specified criteria. For example, filtering a list based on a user-defined true/false expression. You can let the user specify the delegate function to use as a filter to evaluate each list item against.

提交回复
热议问题