本文介绍了为什么委托声明需要命名参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 你好, 我想知道为什么委托声明需要命名参数? 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 解决方案 参数。 编译器本身无法生成任何不相关的名称? 我猜你永远不需要C#中的名字。 Hello, I wonder why the delegate declaration needs named parameters? public delegate void MyDelegate( int a, int b ); // okpublic delegate void MyDelegate( int, int ); // compiler error C allows to define both: typedef void (*MyDelegate)(int); // oktypedef void (*MyDelegate)(int a); // ok in fact, the delegates are only signatures, so the names should beirrelevant. 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, bpublic void a_Method( int x, int y ) {} // names: x, y .... MyDelegate m = new MyDelegate( a_Method ); // ok, but there''s slightconfusion while it could be: public delegate void MyDelegate( int, int ); // a signature onlypublic void a_Method( int x, int y ) {} // names: x, y .... MyDelegate m = new MyDelegate( a_Method ); thanks in advance for enlightening me,Wiktor Zychla 解决方案 parameters. could not the compiler produce any irrelevant names itself?I guess you never need the names in C#. 这篇关于为什么委托声明需要命名参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-28 04:04