Mono C#Compiler和MS C#Compiler关于Scope的区别

Mono C#Compiler和MS C#Compiler关于Scope的区别,第1张

概述我在这里讨论与C#中实例方法/属性的作用域的差异.这是代码: public class Base{ public EventHandler Click {get;set;} public Base(EventHandler clickHandler) { this.Click = clickHandler; }}public class Derived: 我在这里讨论与C#中实例方法/属性的作用域的差异.这是代码:
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#编译器不允许访问派生类的方法/属性等.如何?

解决方法 VS编译器遵循规范.不确定在Mono实现中允许它的原因是什么.

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的区别所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1239639.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-06
下一篇2022-06-06

发表评论

登录后才能评论

评论列表(0条)

    保存