Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this c# syntax mean?

Hi I have a little problem with understanding this kind of syntax

public delegate void DelegateType();
BeginInvoke(new DelegateType(functionName));

Could somebody tell me what exectly mean new DelegateType(functionName). Why do I have to use new keyword ??

like image 447
troubledMan Avatar asked Jun 17 '26 20:06

troubledMan


2 Answers

See the documentation.

A delegate is a type that holds a method.
You're creating a new instance of a delegate type, pointing to an existing method.

C# 2 adds an implicit conversion from a method group to any matching delegate type.
However, since BeginInvoke doesn't take a specific delegate type (such as System.Action), you always need to explicitly create a delegate instance.

like image 108
SLaks Avatar answered Jun 19 '26 11:06

SLaks


The first statement declares a delegate type, the second statement instantiates a new delegate of DelegateType.

From the corresponding MSDN article (read the article for more information!):

Once a delegate type has been declared, a delegate object must be created and associated with a particular method. Like all other objects, a new delegate object is created with a new expression. When creating a delegate, however, the argument passed to the new expression is special — it is written like a method call, but without the arguments to the method.

like image 41
Matten Avatar answered Jun 19 '26 10:06

Matten



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!