
delegate voID D(int x);class C{ public static voID M1(int i) {...} public voID M2(int i) {...}}class Test{ static voID Main() { D cd1 = new D(C.M1); // static method Test t = new C(); // <---- WRONG------- D cd2 = new D(t.M2); // instance method D cd3 = new D(cd2); // another delegate }} 考虑这一行:
Test t = new C();
C类不是从Test类派生的,因此这个赋值不会编译.我在这里遗漏了一些东西(我在文章中没有考虑过一些假设吗?)
即使C类派生自Test,以下行也会出错:
D cd2 = new D(t.M2);
不是吗?
解决方法 这条线应该是C t = new C();
你也可以使用(在新版本的C#中)
var t = new C();
下一行中t.M2有效的唯一方法是if t是否为C类.
总结以上是内存溢出为你收集整理的c# – 这是MSDN中的错误代码吗?全部内容,希望文章能够帮你解决c# – 这是MSDN中的错误代码吗?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)