Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Delegate Instantiation vs. Just Passing the Method Reference [duplicate]

I have a simple question: what's the advantage of instantiating a C# delegate as opposed to just passing the function reference? What I mean is:

Why do:

Thread t = new Thread(new ThreadStart(SomeObject.SomeMethod));

When you can do:

Thread t = new Thread(SomeObject.SomeMethod);

Both will compile and work in my experience...am I missing something?

like image 713
Jrop Avatar asked Feb 02 '10 01:02

Jrop


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.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


2 Answers

As long as the method group SomeObject.SomeMethod has a method with return type void and taking no parameters there is no difference. This is because ThreadStart is defined as a delegate that returns void and takes no parameters and therefore there is an implicit conversion from the method group SomeObject.SomeMethod to ThreadStart. Thus, both are invoking the overload Thread(ThreadStart) of the Thread constructor .

The relevant section of the language specification is §6.6 (Method group conversions).

I have a simple question: what's the advantage of instantiating a C# delegate as opposed to just passing the function reference?

So, just a correction of terminology here. With

class MyObject {
    public void SomeMethod() { }
}

MyObject someObject = new MyObject();

the thing denoted by someObject.SomeMethod is a method group. You can just think of it as the set of overloaded methods can that be looked up using the notation someObject.SomeMethod.

like image 194
jason Avatar answered Oct 13 '22 13:10

jason


The compiler will infer that when you typed the shorter code, you meant the longer code. There's no difference in the ultimate effect. If you want the clarity of the full constructor, you can put it in; if you want the brevity of just the method group, you can allow the compiler to infer the constructor. It's just a stylistic choice.

like image 30
Eric Lippert Avatar answered Oct 13 '22 14:10

Eric Lippert