为什么委托声明需要命名参数? [英] why the delegate declaration needs named parameters?

查看:64
本文介绍了为什么委托声明需要命名参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我想知道为什么委托声明需要命名参数?


public delegate void MyDelegate(int a,int b) ; // ok

public delegate void MyDelegate(int,int); //编译错误


C允许同时定义:

typedef void(* MyDelegate)(int); // ok

typedef void(* MyDelegate)(int a); // ok


实际上,代表只是签名,所以名字应该是

无关紧要。使用无名签名可能更优雅,

因为现在看起来相当混乱:

public delegate void MyDelegate(int a,int b); //姓名:a,b

public void a_Method(int x,int y){} //姓名:x,y


.... MyDelegate m = new MyDelegate(a_Method); //好的,但有一点点

混乱


虽然可能是:


公众代表void MyDelegate(int,int); //仅限签名

public void a_Method(int x,int y){} //姓名:x,y


.... MyDelegate m =新的MyDelegate(a_Method);


提前感谢我的启发,

Wiktor Zychla

解决方案

你好Wiktor,

我猜答案的部分是针对BeginInvoke和EndInvoke

的方法生成的(编译器) )为每个代表 - 命名参数。


-

Miha Markic - RightHand .NET咨询&软件开发

miha at rthand com


" Wiktor Zychla" <即**** @ microsoft.com.no.spam>写在消息

新闻:uT ************** @ TK2MSFTNGP10.phx.gbl ...

你好,
我想知道为什么委托声明需要命名参数?

public delegate void MyDelegate(int a,int b); // ok
public delegate void MyDelegate(int,int); //编译错误

C允许同时定义:

typedef void(* MyDelegate)(int); // ok
typedef void(* MyDelegate)(int a); // ok

实际上,代表只是签名,所以名字应该是无关紧要的。使用无名签名可能会更优雅,因为现在它看起来相当混乱:

公共委托void MyDelegate(int a,int b); //姓名:a,b
public void a_Method(int x,int y){} //姓名:x,y

... MyDelegate m = new MyDelegate(a_Method); //好的,但是有轻微的混乱

虽然它可能是:

公共委托void MyDelegate(int,int); //仅限签名
public void a_Method(int x,int y){} //姓名:x,y

... MyDelegate m = new MyDelegate(a_Method);

提前感谢我的启发,
Wiktor Zychla



我猜:


对于委托声明和实际的

函数声明,他们可能对

括号内的位使用相同的语法规则。可能是为了减少语言语法的大小,尽管可能是因为一些

IDE会自动获取功能签名,并且看到

像func(int iIndex,int iOffset)弹出的东西是比看到func(int,int)更有用的
。 。


C相比之下有很多历史怪癖。以我的

知识来说,这是有效的Ansi C(从1970年代的宿醉中获得的
K& RC):


int double(val)int val; {return val>> 2; }


>我想答案的部分是针对方法BeginInvoke和

EndInvoke

这些是由(编译器)为每个代表生成的 - 用于命名



参数。


编译器本身无法生成任何不相关的名称?

我猜你永远不需要C#中的名字。


Hello,

I wonder why the delegate declaration needs named parameters?

public delegate void MyDelegate( int a, int b ); // ok
public delegate void MyDelegate( int, int ); // compiler error

C allows to define both:

typedef void (*MyDelegate)(int); // ok
typedef void (*MyDelegate)(int a); // ok

in fact, the delegates are only signatures, so the names should be
irrelevant. it could be even more elegant to use nameless signatures,
because now it looks rather confusing:

public delegate void MyDelegate( int a, int b ); // names: a, b
public void a_Method( int x, int y ) {} // names: x, y

.... MyDelegate m = new MyDelegate( a_Method ); // ok, but there''s slight
confusion

while it could be:

public delegate void MyDelegate( int, int ); // a signature only
public void a_Method( int x, int y ) {} // names: x, y

.... MyDelegate m = new MyDelegate( a_Method );

thanks in advance for enlightening me,
Wiktor Zychla

解决方案

Hi Wiktor,

I guess the part of the answer is for the methods BeginInvoke and EndInvoke
which are generated by (compiler) for each delegate - for naming parameters.

--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com

"Wiktor Zychla" <ie****@microsoft.com.no.spam> wrote in message
news:uT**************@TK2MSFTNGP10.phx.gbl...

Hello,

I wonder why the delegate declaration needs named parameters?

public delegate void MyDelegate( int a, int b ); // ok
public delegate void MyDelegate( int, int ); // compiler error

C allows to define both:

typedef void (*MyDelegate)(int); // ok
typedef void (*MyDelegate)(int a); // ok

in fact, the delegates are only signatures, so the names should be
irrelevant. it could be even more elegant to use nameless signatures,
because now it looks rather confusing:

public delegate void MyDelegate( int a, int b ); // names: a, b
public void a_Method( int x, int y ) {} // names: x, y

... MyDelegate m = new MyDelegate( a_Method ); // ok, but there''s slight
confusion

while it could be:

public delegate void MyDelegate( int, int ); // a signature only
public void a_Method( int x, int y ) {} // names: x, y

... MyDelegate m = new MyDelegate( a_Method );

thanks in advance for enlightening me,
Wiktor Zychla



My guess:

They probably use the same grammer rule for the bit within
brackets for both delegate declarations and actual
function declarations. Probably done to reduce the size of
the language grammer, although it might be because some
IDEs automatically pick up function signatures, and seeing
something like func(int iIndex, int iOffset) popping up is
more useful than seeing func(int,int). .

C in contrast has a lot of historical quirks. To my
knowledge, this is valid Ansi C (a hangover from 1970''s
K&R C):

int double(val) int val; { return val>>2; }


> I guess the part of the answer is for the methods BeginInvoke and
EndInvoke

which are generated by (compiler) for each delegate - for naming


parameters.

could not the compiler produce any irrelevant names itself?
I guess you never need the names in C#.


这篇关于为什么委托声明需要命名参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆