Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Update:</p> <blockquote> <p><strong>§ 7.5.7 This access</strong></p> <p>A <em>this-access</em> consists of the reserved word <code>this</code>.</p> <p><em>this-access:</em></p> <pre><code>this </code></pre> <p>A <em>this-access</em> is permitted only in the <em>block</em> of an instance constructor, an instance method, or an instance accessor.</p> </blockquote> <p>This is none of these. The 4.0 compiler looks to be correct. Presumably it isn't happy because this <em>in essence</em> provides access to <code>this</code> at a point when the type isn't initialized. Maybe ;-p</p> <p>Note that I expect that it isn't really the <code>this.someField</code> that causes this - more that the <em>use</em> of a field causes <code>this</code> to be <em>captured</em>, meaning it wants to hoist the <code>this</code> instance onto a compiler-generated class - as though you had written:</p> <pre><code>public MyCtor() : base( new SomeType(this).SomeMethod ) {...} </code></pre> <p>The C# 3.0 compiler spots the above abuse of <code>this</code>.</p> <hr> <p>Reproduced. Investigating. It looks like an issue resolving the implicit <code>this</code> in the constructor chaining.</p> <p>The most likely workaround would be to use a <code>virtual</code> method instead of a delegate, and simply override it in the derived class.</p> <p>One workaround would be to pas the instance in as an argument, so the delegate becomes "obj => obj.whatever...", and use <code>theDelegate(this);</code>. </p> <p>Simpler repro:</p> <pre><code>public class MyBase { public MyBase(Action a) { } } public class MySub : MyBase { private string foo; // with "this.", says invalid use of "this" // without "this.", says instance required public MySub() : base(delegate { this.foo = "abc"; }) { } } </code></pre> <p>I would need to check the spec, but I'm not sure whether <code>this</code> <strong>is</strong> valid in this context... so the 4.0 compiler could be correct.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload