Difference between Mono C # compiler and MS C # compiler relative to scope

In this case, I came across a corner case, taking into account the difference with the scope of the methods / properties of the instance in C #. Here is the code:

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() { } } 

The code compiles in MonoDevelop 3.0, but gives an error message in VS2010: An object reference is required for a non-static field, method or property "Base.Execute" Basically, it comes down to the fact that when you call the base class constructor from the constructor of the derived class MS C # the compiler does not allow access to methods / properties of a derived class, etc. Like this?

+7
source share
1 answer

The VS compiler follows the specifications. Not sure what the reason is for Mono's continuity.

C # Specification , Section 10.11.1 Constructor Initializers:

The instance constructor initializer cannot access the instance being created. Therefore, it is a compile-time error to refer to this in the constructor initializer argument expression, as well as a compile-time error for the argument expression to refer to any instance member through a simple name.

+7
source

All Articles