Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There's no way around it, the C# specification explicitly says that <a href="http://msdn.microsoft.com/en-us/library/ms364047(VS.80).aspx#cs3spec_topic5" rel="noreferrer">"It is not possible for an object or collection initializer to refer to the object instance being initialized."</a></p> <p>As for why it's impossible, I suspect that there's just no nice way to implement it. We want some syntactic sugar equivalent to</p> <pre><code>var temp = new TestClass(); temp.Id = 1; temp.SomeProperty = SomeMethod(temp); x = temp; </code></pre> <p>We just need a keyword to refer to <code>temp</code> within the initializer, but none is easily available. We can't use <code>this</code> because it already means something outside the initializer. Should <code>SomeProperty = this.SomeMethod(this)</code> be equivalent to <code>temp.SomeProperty = this.SomeMethod(temp)</code> or <code>temp.SomeProperty = temp.SomeMethod(temp)</code>? The second is consistent, but then what happens if we need the first?</p> <p>We could try to use <code>x</code>, though we can only pick a name if the new object is immediately assigned to a variable. However, we now can't refer to the old value of <code>x</code> inside the initializer, doing the equivalent of <code>temp.SomeProperty = SomeMethod(x)</code>.</p> <p>We could reuse the <code>value</code> keyword from property setters. This sounds good since <code>value</code> already stands in for the missing parameter if you consider a property getter to be syntactic sugar for a <code>set_SomeProperty(value)</code> method. Using it to also refer to the missing variable in the object initializer looks promising. However, we could be creating this object inside a property setter, in which case <code>value</code> is already being used, and we need to be able to do <code>temp.SomeProperty = SomeMethod(value)</code>.</p> <p>It looks like we'll have to create a new keyword just for this purpose, maybe <code>newthis</code>. However, this is a breaking change to the language because any code that has a variable called <code>newthis</code> doesn't work any more. Microsoft generally needs a really good reason to introduce breaking changes, so it's better to forbid access to the object being initialized.</p>
    singulars
    1. This table or related slice is empty.
    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