Click here to Skip to main content
16,005,037 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What feature of C# is this? I realize it is initializing objects
What is this called in the C# specification?

Employee JohnDoe = new Employee()
{
firstname = "john";
lastname = "doe";
age = 200;
h1visa = false;
};

but what is the reason for this vs the C++ way of initializing default constructors

Employee JohnDoe = new Employee(john, doe, 200, false)


thanks in advance
Posted
Updated 5-Sep-15 6:03am
v3
Comments
Sergey Alexandrovich Kryukov 5-Sep-15 12:23pm    
I'm just curious: if you understand how it works, what's the great importance of the name? This is not so special feature to be names specially. No, I don't blame you: it's good to use proper terminology, but — what's wrong with just reading original MSDN documentation.

Also, aren't the benefits of this syntax quite obvious. (I want to say "if you don't like it, don't use it".) I would say: good features don't need "excuses". :-)

—SA

MSDN covers this pretty well: https://msdn.microsoft.com/en-us/library/bb384062.aspx?f=255&MSPPError=-2147217396[^]

"Object initializers let you assign values to any accessible fields or properties of an object at creation time without having to invoke a constructor followed by lines of assignment statements. The object initializer syntax enables you to specify arguments for a constructor or omit the arguments (and parentheses syntax).
...
Although object initializers can be used in any context, they are especially useful in LINQ query expressions. Query expressions make frequent use of anonymous types, which can only be initialized by using an object initializer"
 
Share this answer
 
Please see my comment to the question. I really don't understand what would be so hard to find out the terminology. It's just called "initializer", nothing special; this is the term you already used yourself (credit to you :)): https://msdn.microsoft.com/en-us/library/bb384062.aspx[^].

Aren't the benefits of this syntax obvious? First of all, it can be considered as some equivalent of passing parameters by name, without having any parameters, though. Frankly, quickly writing the call to a constructor with more than a couple of parameters is somewhat bothering, especially if there are many different constructors with different number of parameters, even if you use Intellisense; you need to write all parameters in certain order, and have to compare different constructors to make a decision which one you really need, often in the middle of the process of filling in those parameters.

With newer syntax, you can write the set of values of some properties in arbitrary order. When you look at your own or someone else's code, you can clearly see which value goes to what property. When you design the constructors, you can avoid constructors with too many parameters, leaving only the most important cases. Even when you, by some reason, compile the code targeting the earlier C# versions when this constructor, you can always leave the user with the opportunity to assign the property values, not passed as constructor parameters, using assignment statements, but the user of the language with this newer form of the initializer can really enjoy it's benefits.

Really, I feel uncomfortable explaining the rationale of the feature. It resembles the awkward feeling of explaining the joke to those who cannot get it — not the best activity. As I said, "good feature does not need excuses".

—SA
 
Share this answer
 
thanks

"arbitrary order" is the key functionality I didn't know about

thanks
 
Share this answer
 
v2

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