
public class Base{ public EventHandler Click {get;set;} public Base(EventHandler clickHandler) { this.Click = clickHandler; }}public class Derived: Base{ public Derived(): base((sender,e) => Execute()) { } private voID Execute() { }} 代码在MonoDevelop 3.0上编译得很好,但在VS2010中给出了一个错误:
非静态字段,方法或属性“Base.Execute”需要对象引用
基本上,它归结为这样一个事实:当从派生类的构造函数调用基类的构造函数时,MS的C#编译器不允许访问派生类的方法/属性等.如何?
C# Specification,第10.11.1节构造函数初始值设定项:
总结An instance constructor initializer cannot access the instance being created. Therefore it is a compile-time error to reference this in an argument Expression of the constructor initializer,as is it a compile-time error for an argument Expression to reference any instance member through a simple-name.
以上是内存溢出为你收集整理的Mono C#Compiler和MS C#Compiler关于Scope的区别全部内容,希望文章能够帮你解决Mono C#Compiler和MS C#Compiler关于Scope的区别所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)