What does C # syntax mean?

Hi I have a little problem understanding this syntax

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

Can someone tell me what the new type of DelegateType (functionName) means. Why do I need to use a new keyword

+3
source share
4 answers

See the documentation.

A delegate is a type that contains a method.
You create a new instance of the delegate type, pointing to an existing method.

C # 2 adds an implicit conversion from a group of methods to any appropriate delegate type.
However, since it BeginInvokedoes not accept a specific type of delegate (e.g. System.Action), you always need to explicitly create an instance of the delegate.

+6
source

, DelegateType.

MSDN ( !):

, - . , - . - , .

+1
 public delegate void DelegateType();

. , .

BeginInvoke, . # System.Delegate, ( System.Delegate), .

:

new DelegateType(functionName)

(DelegateType), .

Often, new APIs will use a known type, such as System.Action (which has the same syntax as your DelegateType). If the method takes an “action”, you will not need the definition above, and you could do:

CallMethodTakingAction(functionName);
+1
source

'DelegateType' is just a thing, therefore, like any other type, you want to say “here is one instance of this type”, you need to use “new”.

0
source

Source: https://habr.com/ru/post/1785775/


All Articles