title: Initializing events with initializer syntax
body: <p>I often want to write something like this:</p>
  
  <pre><code>new Form
  {
      Text = "Caption",
      Controls =
      {
          new Button { Text = "Button 1", Click = (s, e) =&gt; MessageBox.Show("Button 1 Clicked"), Location = new Point(10, 10) },
          new Button { Text = "Button 2", Click = new EventHandler(Button2Clicked), Location = new Point(10, 40) },
          new Button { Text = "Button 3", Click = Button3Clicked, Location = new Point(10, 70) },
      },
  }
  </code></pre>
  
  <p>Initializer syntax is just sugar, so why can't the compiler figure out how to generate code for an event subscription?</p>
  
  <p>Gimme some sugar, baby!</p>
  
  <p>When initializer syntax was invented, someone must have thought about events and rejected them. I've been trying to imagine what the rationale might have been and am coming up blank.</p>
  
  <p>Is it because an event is a multi-cast object that might have more than one subscriber? No, this is an initialization process; There can be no other subscribers. <strong>[Updated]</strong> Not true, initializers are applied post-construction and an object can <a href="http://tergiver.wordpress.com/2011/01/20/self-subscription-is-asinine/" rel="noreferrer">subscribe to its own events</a>.</p>
  
  <p>A note to Eric: I've heard the <em>Why doesn't C# implement feature X</em> speech. In this case, someone was already there, implementing initializers.</p>
  
  <p><strong>Updated</strong></p>
  
  <p>There seems to be contention/confusion because I used <code>Click =</code> in my example. The actual syntax is not relevant to the question. It could just as easily be <code>Click +=</code> which mirrors the way you have to add a handler normally. I prefer the former because it's consistant with the rest of the initializer syntax, but ultimately I don't care, just so long as I can subscribe to an event in an initializer list.</p>
  
  <p><strong>Another Update</strong></p>
  
  <p>I do realize that adding the feature now is probably unlikely. The first issue that comes to mind is that Intellisense has to be updated. There are probably many other things that would hinder adding this feature now. My question is: Why didn't they add it in the first place. There must have been something compelling that warrented the 'nay' vote.</p>