Click here to Skip to main content
15,886,046 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
this.Add(new TabPage
{
    Text = text
});


I saw this after decompiling the source for TabControl. I can sort of infer what it's doing, but I've never seen an object being created like that before.

C#
new TabPage { Text = text };
Posted
Comments
Sergey Alexandrovich Kryukov 30-Jan-12 17:16pm    
So what? What is your question? Any concerns about it? Why?
--SA
agent154 30-Jan-12 17:29pm    
I never said there was a problem with it. I just haven't seen it before.

Well, now you see it. What's so special about it? :-)

Please see it in the specification of C# 3.0 (I believe this syntax was introduced in this version): http://msdn.microsoft.com/en-us/library/ms364047%28v=vs.80%29.aspx#cs3spec_topic5[^], locate the section "Object Initializers".

This is a pretty funny discussion related to coding styles using this feature: http://stackoverflow.com/questions/2574725/is-it-bad-to-use-initializer-block[^].

I personally think there is nothing wrong in it, even though I understand concerns of those warning against the practice of using those block object initializers. Many language features can be abused or, just the opposite, reasonably used, depending on the situation.

—SA
 
Share this answer
 
Comments
Espen Harlinn 30-Jan-12 17:25pm    
Reasonable reply, absolutely deserving a 5 in this situation :)
Sergey Alexandrovich Kryukov 30-Jan-12 17:27pm    
Thank you, Espen.
--SA
Sander Rossel 30-Jan-12 17:29pm    
Nice discussion. The msdn article is a hard read due to the format. Good links though.
My 5!
Sergey Alexandrovich Kryukov 30-Jan-12 17:34pm    
Thank you Naerling.
You know, getting precise information is always more difficult than something vague, but more valuable... :-)
Anyway, you added very adequate code samples explaining things.
Teamwork!
--SA
Sander Rossel 30-Jan-12 17:39pm    
Nice working with you! :)
Actually, it's a 'shortcut' to setting some Properties directly after initialization.
Consider this:
C#
// Instead of doing:
Person p = new Person();
p.Name = "Naerling";
p.LastName = "Hell yeah!";
p.Age = 24;
// Or...
Person p = new Person() { Name = "Naerling", LastName = "Hell yeah!", Age = 24 };
// And then...
this.AddPersonToPhoneBook(p);

// You could be doing:
this.AddPersonToPhoneBook(new Person() { Name = "Naerling", LastName = "Hell yeah!", Age = 24 });

Personally I never use this syntax. I have no good reason not to though.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 30-Jan-12 17:25pm    
Correct, my 5.
About use or not -- please see my answer (why not?).
--SA
Sander Rossel 30-Jan-12 17:31pm    
Thanks :)
Espen Harlinn 30-Jan-12 17:25pm    
5'ed! :)
Sander Rossel 30-Jan-12 17:31pm    
Thanks! :)
Sergey Alexandrovich Kryukov 30-Jan-12 17:26pm    
What I really like is the possibility to add ',' after the last element (same in array initializers). It really improves maintenance.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900