Click here to Skip to main content
13,052,163 members (40,616 online)
Rate this:
Please Sign up or sign in to vote.
See more:
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 10:48am
SAKryukov 30-Jan-12 17:16pm
So what? What is your question? Any concerns about it? Why?
agent154 30-Jan-12 17:29pm
I never said there was a problem with it. I just haven't seen it before.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

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):[^], locate the section "Object Initializers".

This is a pretty funny discussion related to coding styles using this feature:[^].

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.

Espen Harlinn 30-Jan-12 17:25pm
Reasonable reply, absolutely deserving a 5 in this situation :)
SAKryukov 30-Jan-12 17:27pm
Thank you, Espen.
Naerling 30-Jan-12 17:29pm
Nice discussion. The msdn article is a hard read due to the format. Good links though.
My 5!
SAKryukov 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.
Naerling 30-Jan-12 17:39pm
Nice working with you! :)
SAKryukov 30-Jan-12 18:29pm
Thank you. Likewise. :-)
BillWoodruff 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 30-Jan-12 23:06pm
Agree; thank you, Bill.
Rate this: bad
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...
// 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.
SAKryukov 30-Jan-12 17:25pm
Correct, my 5.
About use or not -- please see my answer (why not?).
Naerling 30-Jan-12 17:31pm
Thanks :)
Espen Harlinn 30-Jan-12 17:25pm
5'ed! :)
Naerling 30-Jan-12 17:31pm
Thanks! :)
SAKryukov 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.
Naerling 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 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.
Naerling 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 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:
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.

Naerling 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 :)

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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 30 Jan 2012
Copyright © CodeProject, 1999-2017
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