Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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.
 
new TabPage { Text = text };
Posted 30-Jan-12 11:48am
Comments
SAKryukov at 30-Jan-12 17:16pm
   
So what? What is your question? Any concerns about it? Why?
--SA
agent154 at 30-Jan-12 17:29pm
   
I never said there was a problem with it. I just haven't seen it before.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Well, now you see it. What's so special about it? Smile | :)
 
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
  Permalink  
Comments
Espen Harlinn at 30-Jan-12 17:25pm
   
Reasonable reply, absolutely deserving a 5 in this situation :)
SAKryukov at 30-Jan-12 17:27pm
   
Thank you, Espen.
--SA
Naerling at 30-Jan-12 17:29pm
   
Nice discussion. The msdn article is a hard read due to the format. Good links though.
My 5!
SAKryukov at 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
Naerling at 30-Jan-12 17:39pm
   
Nice working with you! :)
SAKryukov at 30-Jan-12 18:29pm
   
Thank you. Likewise. :-)
--SA
BillWoodruff at 30-Jan-12 22:20pm
   
Very reasonable answer, SAK, +5. I have made limited use of this new syntax, so far (old habits are resistant to change), but it does make code more readable: just looking at the parameter list alone of a method call, you know the names of the variables.
 
I'd highly recommend using this for beginners, or, if I were teaching, for students, as a useful self-discipline.
SAKryukov at 30-Jan-12 23:06pm
   
Agree; thank you, Bill.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Actually, it's a 'shortcut' to setting some Properties directly after initialization.
Consider this:
// 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.
  Permalink  
Comments
SAKryukov at 30-Jan-12 17:25pm
   
Correct, my 5.
About use or not -- please see my answer (why not?).
--SA
Naerling at 30-Jan-12 17:31pm
   
Thanks :)
Espen Harlinn at 30-Jan-12 17:25pm
   
5'ed! :)
Naerling at 30-Jan-12 17:31pm
   
Thanks! :)
SAKryukov at 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
Naerling at 30-Jan-12 17:31pm
   
Never noticed that! Wouldn't know why that's possible... Except that if you'd ever add a Property you'd already have the , which might really come in handy if you're in a hurry!
SAKryukov at 30-Jan-12 17:37pm
   
Not even in a hurry. It makes editing more smooth, cause less mistakes.
There was a long-lasting competition between separator-not-terminator syntax (Pascal, which is much stronger in many aspects of syntax compared with C, but not in this aspect) and terminator-not-separator (C/C++, but by some reason only for ';' in statements). Finally we have nearly optimal syntax.
--SA
Naerling at 30-Jan-12 17:44pm
   
Not sure I understand... I'm a VB programmer, we don't have terminators :)
So why would I create an array like this:
int[] i = new[] {1, 2, 3, 4,}; // Notice the , at the end.
instead of
int[] i = new[] {1, 2, 3, 4};
If I would never need to edit this I would just add 5 to the first one and , 5 to the last one...
SAKryukov at 30-Jan-12 18:28pm
   
I don't leave in California, so we also don't have Terminators!
 
First, of all, a little fix to you answer: "Shortcut for setting some Properties". And Fields, too. Even though non-private fields are not recommended, they can be used, too.
 
Now, here is the reason. This is nothing but pure support during editing.
 
You have:
{
A,
B,
C,
}
To shuffle C into different position or add D, you only shuffle lines or add a line. I last comma was not allowed, you also needed to add one comma and add another one. Cumbersome. Same thing -- in one line. Easier to edit, all members are identical. Easier to generate code.
 
--SA
Naerling at 30-Jan-12 18:41pm
   
Ah, that makes sense.
It's the reason why I put the comma in front of any fields in select or update statements in SQL :)
 
select
id
,name
,lastname
from person
Now if I want to add age or delete lastname I only need to edit one row :)
Thanks for the example!

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



Advertise | Privacy | Mobile
Web02 | 2.8.1411022.1 | Last Updated 30 Jan 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100