Why does a delegate declaration require an identifier?

When I skip an identifier with a delegation type, the compiler throws an error requiring an identifier. So, when declaring a delegate, why do I need to specify a type identifier? Having only type information is enough in the declaration?

public delegate void MyDel(object o, EventArgs e); // accepted by compiler
public delegate void MyDel(object, EventArgs); // throws error, why?

NOTE. C ++ only supports declarations with type. Since I come from a C ++ background, I expected the same behavior here.

+4
source share
5 answers

If nothing else, so while you are writing documentation , you can clearly indicate which parameter you are discussing. (For example, for delegates with multiple parameters of the same type)

( ), , .

+1

MyDel myDel = MyMethod;
myDel(o:sender,e: eve);

# , , .

+1

, . - .

, , . object object sender, , . sender , , .

-, IDE . - Windows Forms Designer ? . , IDE .

, , , , , , .:)

0

, , :

, , (, ). . , , , . "" . , , . :

void PropertyChangedHandler(object sender, PropertyChangedEventArgs e) {
    //...
}

:

delegate void PropertyChangedEventHandler(object sender, PropertyChangedEventArgs e);

, - delegate (, , , , ). .

Visual Studio , +=, Tab , . . , , param1, param2 .., . , , , IntelliSense . , .

0

Any good programmer will identify parameter names, clearly indicating what they are for. If only a type is allowed, this leads to confusion. This confusion requires documentation and careful reading, resulting in loss of performance. To do this, the language developers decided to define the parameter names when defining a delegate or interface.

Imagine SQLCommand defn

SqlCommand(string, SqlConnection)

against

SqlCommand(string cmdText, SqlConnection connection)
-1
source

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


All Articles