C#新[代表]没有必要? [英] C# new [delegate] not necessary?
问题描述
我一直在玩的HttpWebRequest
取值最近,和教程他们总是这样:
I've been playing with HttpWebRequest
s lately, and in the tutorials they always do:
IAsyncResult result = request.BeginGetResponse(
new AsyncCallback(UpdateItem),state);
但新的AsyncCallback
似乎不是necesary。如果的updateItem
具有正确的签名,则似乎没有成为一个问题。那么,为什么人们有吗?是否在所有做什么?
But new AsyncCallback
doesn't seem to be necesary. If UpdateItem
has the right signature, then there doesn't seem to be a problem. So why do people include it? Does it do anything at all?
推荐答案
这是同样的事情,主要是(也有少数超载规则来想想,虽然未在这个简单的例子)。但在C#以前的版本中,没有任何委托类型推断。所以这本手册是要么(一)笔试前,委托类型推断可用,或(b),他们希望能详细解释之用。
It's the same thing, mostly (there are a few overload rules to think about, although not in this simple example). But in previous versions of C#, there wasn't any delegate type inference. So the tutorial was either (a) written before delegate type inference was available, or (b) they wanted to be verbose for explanation purposes.
下面是一些摘要方式的不同可以采取委托类型推断的优势:
Here's a summary of a few of the different ways you can take advantage of delegate type inferencing:
// Old-school style.
Chef(new CookingInstructions(MakeThreeCourseMeal));
// Explicitly make an anonymous delegate.
Chef(delegate { MakeThreeCourseMeal });
// Implicitly make an anonymous delegate.
Chef(MakeThreeCourseMeal);
// Lambda.
Chef(() => MakeThreeCourseMeal());
// Lambda with explicit block.
Chef(() => { AssembleIngredients(); MakeThreeCourseMeal(); AnnounceDinnerServed(); });
这篇关于C#新[代表]没有必要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!