与创建中间方法相比,使用委托有什么优势?
有没有理由使用这个:
bool flag;
public Form1()
{
if (flag) byDelegate = square;
else byDelegate = cube;
Text = byDelegate(3).ToString();
}
int square(int i) { return i * i; }
int cube(int i) { return i * i * i; }
delegate int delegate1(int x);
delegate1 byDelegate;
而不是:
bool flag;
public Form2()
{
Text = fakeDelegate(3).ToString();
}
int square(int i) { return i * i; }
int cube(int i) { return i * i * i; }
int fakeDelegate(int i)
{
if (flag) return square(i);
else return cube(i);
}
谢谢。
Is there a reason to use this:
bool flag;
public Form1()
{
if (flag) byDelegate = square;
else byDelegate = cube;
Text = byDelegate(3).ToString();
}
int square(int i) { return i * i; }
int cube(int i) { return i * i * i; }
delegate int delegate1(int x);
delegate1 byDelegate;
Rather than:
bool flag;
public Form2()
{
Text = fakeDelegate(3).ToString();
}
int square(int i) { return i * i; }
int cube(int i) { return i * i * i; }
int fakeDelegate(int i)
{
if (flag) return square(i);
else return cube(i);
}
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
委托通常异步用于事件或传递到方法/类型中,以便稍后可以调用委托指向的方法(“函数指针”)。在您的情况下,看起来没有任何优势,因为您正在同步执行所有操作。
例如,
您通常不需要在 3.5 及以上版本中声明自己的委托,除非您希望代码通过这些声明提供更多含义。
Action
类型和Func
类型(func 提供带有返回值的方法)可以节省您的精力。Delegates are usually used asynchronously, for events or passed into methods/types so that the method the delegate points to ('function pointers') can be called later. In your case there looks like there's no advantage as you're doing everything synchronously.
For example
You generally don't need to declare your own delegates in 3.5 upwards unless you want to your code to provide a bit more meaning via those declarations. The
Action
type and theFunc
type (func provides a method with a return value) save you the effort.为了解决您提到的确切情况,通过显式使用委托,您将使事情变得过于复杂,如果您不真正了解幕后发生的事情(我并不是说您不了解),情况就会变得更加复杂。 ..
更一般地,您可以将委托视为“契约”(或者如果您的背景是 C/C++,则视为“函数指针”):您告诉编译器在该位置期望函数接收给定的参数列表并提供给定的输出(可能
void
),但您没有告诉它该函数的真正用途。这将方法的主体与函数的实际实现“解耦”,使您可以自由地从代码的不同部分传递多个不同的实现。this 似乎有相当好的、更详细的解释 主题。
To address the precise situation you mentioned, you'll be over-complicating things by explicitly using a delegate, and even more if you don't really understand what's going on behind the scenes (I'm not saying you don't)...
More generally, you can think of a delegate as a "contract" (or as a "pointer to a function" if your background is in C/C++): you are telling the compiler to expect in that place a function receiving a given list of parameters and providing a given output (possibly
void
), but you don't tell it what the function really does. That "decouples" the method's body from the real implementation of the function, giving you the freedom to pass several different implementations from different parts of your code.There seem to be pretty good, more elaborated explanations at this topic.
在您描述的情况下,我认为使用委托没有任何优势。事实上恰恰相反,因为如果没有其他事情的话,它会让事情看起来更复杂。
使用委托的原因是,例如,如果您希望允许外部代码指定要调用的函数,并且您事先不知道可能指定的所有函数。
In the situation you describe I don't see any advantage to using delegates. Quite the opposite in fact since it if nothing else makes things look more complicated.
The reason to use delegates would be for example if you want to allow outside code to specify the function to call and if you don't know in advance all the functions that may be specified.
fakeDelegate根本不是委托:它只是一个方法调用(进而调用其他两个方法之一)。
现在,委托(包括 lambda 和“匿名函数”)非常有用的两个原因是:
委托允许将函数(或方法)视为一流值。帖子中的第一个例子就是这样做的,尽管这是一个愚蠢的例子(我有时会这样做,但需要更大的背景来论证一种形式相对于另一种形式的有效性)。更常见的是,我将此属性用于类似于
IDictionary
的数据结构,然后将其用作map["foo"](bar)
或类似——如果没有一等函数值的大丑陋调度(或反射),就无法做到这一点!/new/ 委托可以创建闭包——也就是说,它可以绑定当前词法范围内的自由变量。这是用普通方法无法做到的;即使方法组可转换为委托,
cube
和square
都不会创建闭包。虽然上面的示例没有任何优势,但当“只需将额外的一点信息传递给回调”时,这可能非常方便。由于这两种情况都没有在示例中使用(特别好),那么......
快乐编码。
fakeDelegate
is not a delegate at all: it is just a method call (that in turn calls one of two other methods).Now, two reasons where delegates -- including lambdas and "anonymous functions" -- are very useful are:
Delegates allow a function (or method) to treated as a first-class value. The first example in the post does this, even though it is a silly example (I do this sometimes, but a larger context would be required to argue the validity of one form over the other). More often, I use this property for a data-structure that looks like
IDictionary<String,MyDelegate>
which is then used asmap["foo"](bar)
or similar -- can't do this without a big ugly dispatch (or reflection) without first-class functions values!A /new/ delegate can create a closure -- that is, it can bind over free variables in the current lexical scope. This cannot be done with normal methods; neither
cube
norsquare
will create a closure, even though the method group is convertible to a delegate. While there is no advantage in the example above, this can be very handy when "just needing to pass that extra little bit of information" to a callback.Since neither of these two cases are (particularly well) used in the example then....
Happy coding.