Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# new [delegate] not necessary?

Tags:

c#

delegates

I've been playing with HttpWebRequests lately, and in the tutorials they always do:

IAsyncResult result = request.BeginGetResponse(
  new AsyncCallback(UpdateItem),state);

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?

like image 612
mpen Avatar asked Dec 27 '09 07:12

mpen


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C language?

C is an imperative procedural language supporting structured programming, lexical variable scope, and recursion, with a static type system. It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support.


3 Answers

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(); });
like image 160
John Feminella Avatar answered Oct 03 '22 09:10

John Feminella


AsyncCallback is just a delegate in C#, it is declared as

public delegate void AsyncCallback(IAsyncResult ar);

When you pass the method name itself as long as the signature matches the compiler will usually substitute the code for you, its just shortcut.

You can simply check this using Reflector. If you have this for example.

request.BeginGetResponse(TestMethod, null);

 static void (IAsyncResult r)
        {
           //do something
        }

The compiled code will actually look like this.

   request.BeginGetResponse(new AsyncCallback(Test), null);
like image 45
Stan R. Avatar answered Oct 03 '22 09:10

Stan R.


For completeness, this changes between C# 1.2 (with .NET 1.1) and C# 2.0 (with .NET 2.0). So from 2.0 onwards you can indeed omit the new SomeDelegateType(...) in most scenarios. Oddly, the tooling hasn't changed, so in the IDE if you type someObj.SomeEvent += the IDE will suggest (via tab tab) the full version including delegate type.

like image 25
Marc Gravell Avatar answered Oct 03 '22 11:10

Marc Gravell