Object initialization vs property assignment [duplicate]Is there any benefit of using an Object Initializer?Setting properties via object initialization or not : Any difference ?How do you give a C# Auto-Property a default value?Deep cloning objectsWhat is the difference between a field and a property?LINQ's Distinct() on a particular propertyDoes C# have extension properties?Get property value from string using reflection in C#Deserialize JSON into C# dynamic object?How to Sort a List<T> by a property in the objectAll possible array initialization syntaxesGood or bad practice? Initializing objects in getter
Gofer work in exchange for Letter of Recommendation
Does C++20 mandate source code being stored in files?
Why is the name Bergson pronounced like Berksonne?
Have only girls been born for a long time in this village?
Unsolved Problems due to Lack of Computational Power
Can I submit a paper computer science conference using an alias if using my real name can cause legal trouble in my original country
Why do balloons get cold when they deflate?
Where is this New York City Broadway location from Fall 1958?
How to detect a failed AES256 decryption programmatically?
Starships without computers?
What is the evidence on the danger of feeding whole blueberries and grapes to infants and toddlers?
Can others monetize my project with GPLv3?
Why doesn't mathematics collapse down, even though humans quite often make mistakes in their proofs?
Control GPIO pins from C
How to translate 脑袋短路 into English?
My new Acer Aspire 7 doesn't have a Legacy Boot option, what can I do to get it?
Just one file echoed from an array of files
iPad or iPhone doesn't charge until unlocked?
Peterhead Codes and Ciphers Club: Weekly Challenge
Is "stainless" a bulk or a surface property of stainless steel?
When does The Truman Show take place?
Are there categories whose internal hom is somewhat 'exotic'?
How to shade a polygon with curved lines in tikz?
Use of vor in this sentence
Object initialization vs property assignment [duplicate]
Is there any benefit of using an Object Initializer?Setting properties via object initialization or not : Any difference ?How do you give a C# Auto-Property a default value?Deep cloning objectsWhat is the difference between a field and a property?LINQ's Distinct() on a particular propertyDoes C# have extension properties?Get property value from string using reflection in C#Deserialize JSON into C# dynamic object?How to Sort a List<T> by a property in the objectAll possible array initialization syntaxesGood or bad practice? Initializing objects in getter
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
This question already has an answer here:
Setting properties via object initialization or not : Any difference ?
5 answers
Is there any benefit of using an Object Initializer?
5 answers
This morning, I had a discussion with a co-worker regarding the following two example of initializing the code.
Example 1:
var employee = new Employee();
employee.FirstName = GetFirstName(); // this may get values from db
Example 2:
var employee = new Employee()
FirstName = GetFirstName()
My co-worker's argument was that example 2 is more efficient and if that object has any errors during initialization, it is automatically removed from heap. I am thinking that the compiled version of the code would look similar to example 1.
My question is that is my co-worker correct in his argument? When should you use property assignment instead of object initialization? Are there any best practices around this? MS doesn't have anything ...
c#
marked as duplicate by Owen Pauling, Hans Kesting
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 27 at 14:14
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
|
show 1 more comment
This question already has an answer here:
Setting properties via object initialization or not : Any difference ?
5 answers
Is there any benefit of using an Object Initializer?
5 answers
This morning, I had a discussion with a co-worker regarding the following two example of initializing the code.
Example 1:
var employee = new Employee();
employee.FirstName = GetFirstName(); // this may get values from db
Example 2:
var employee = new Employee()
FirstName = GetFirstName()
My co-worker's argument was that example 2 is more efficient and if that object has any errors during initialization, it is automatically removed from heap. I am thinking that the compiled version of the code would look similar to example 1.
My question is that is my co-worker correct in his argument? When should you use property assignment instead of object initialization? Are there any best practices around this? MS doesn't have anything ...
c#
marked as duplicate by Owen Pauling, Hans Kesting
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 27 at 14:14
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
2
They're literally the same thing. Check the IL.
– Amy
Mar 27 at 14:08
Generated code is the same, while example 1 has better (more exact) line numbers if an exception occurs.
– Caramiriel
Mar 27 at 14:08
This is a compiler feature, not a run-time feature
– Erno de Weerd
Mar 27 at 14:09
@Caramiriel I forgot to mention in my orig post but that was my counter argument too.
– Skadoosh
Mar 27 at 14:09
1
Just go to the C# spec. 12.7.11.3 Object initializers: "A member initializer that specifies an expression after the equals sign is processed in the same way as an assignment (§12.18.2) to the field or property."
– Raymond Chen
Mar 27 at 14:12
|
show 1 more comment
This question already has an answer here:
Setting properties via object initialization or not : Any difference ?
5 answers
Is there any benefit of using an Object Initializer?
5 answers
This morning, I had a discussion with a co-worker regarding the following two example of initializing the code.
Example 1:
var employee = new Employee();
employee.FirstName = GetFirstName(); // this may get values from db
Example 2:
var employee = new Employee()
FirstName = GetFirstName()
My co-worker's argument was that example 2 is more efficient and if that object has any errors during initialization, it is automatically removed from heap. I am thinking that the compiled version of the code would look similar to example 1.
My question is that is my co-worker correct in his argument? When should you use property assignment instead of object initialization? Are there any best practices around this? MS doesn't have anything ...
c#
This question already has an answer here:
Setting properties via object initialization or not : Any difference ?
5 answers
Is there any benefit of using an Object Initializer?
5 answers
This morning, I had a discussion with a co-worker regarding the following two example of initializing the code.
Example 1:
var employee = new Employee();
employee.FirstName = GetFirstName(); // this may get values from db
Example 2:
var employee = new Employee()
FirstName = GetFirstName()
My co-worker's argument was that example 2 is more efficient and if that object has any errors during initialization, it is automatically removed from heap. I am thinking that the compiled version of the code would look similar to example 1.
My question is that is my co-worker correct in his argument? When should you use property assignment instead of object initialization? Are there any best practices around this? MS doesn't have anything ...
This question already has an answer here:
Setting properties via object initialization or not : Any difference ?
5 answers
Is there any benefit of using an Object Initializer?
5 answers
c#
c#
asked Mar 27 at 14:06
SkadooshSkadoosh
1,4067 gold badges32 silver badges50 bronze badges
1,4067 gold badges32 silver badges50 bronze badges
marked as duplicate by Owen Pauling, Hans Kesting
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 27 at 14:14
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Owen Pauling, Hans Kesting
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 27 at 14:14
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Owen Pauling, Hans Kesting
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 27 at 14:14
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
2
They're literally the same thing. Check the IL.
– Amy
Mar 27 at 14:08
Generated code is the same, while example 1 has better (more exact) line numbers if an exception occurs.
– Caramiriel
Mar 27 at 14:08
This is a compiler feature, not a run-time feature
– Erno de Weerd
Mar 27 at 14:09
@Caramiriel I forgot to mention in my orig post but that was my counter argument too.
– Skadoosh
Mar 27 at 14:09
1
Just go to the C# spec. 12.7.11.3 Object initializers: "A member initializer that specifies an expression after the equals sign is processed in the same way as an assignment (§12.18.2) to the field or property."
– Raymond Chen
Mar 27 at 14:12
|
show 1 more comment
2
They're literally the same thing. Check the IL.
– Amy
Mar 27 at 14:08
Generated code is the same, while example 1 has better (more exact) line numbers if an exception occurs.
– Caramiriel
Mar 27 at 14:08
This is a compiler feature, not a run-time feature
– Erno de Weerd
Mar 27 at 14:09
@Caramiriel I forgot to mention in my orig post but that was my counter argument too.
– Skadoosh
Mar 27 at 14:09
1
Just go to the C# spec. 12.7.11.3 Object initializers: "A member initializer that specifies an expression after the equals sign is processed in the same way as an assignment (§12.18.2) to the field or property."
– Raymond Chen
Mar 27 at 14:12
2
2
They're literally the same thing. Check the IL.
– Amy
Mar 27 at 14:08
They're literally the same thing. Check the IL.
– Amy
Mar 27 at 14:08
Generated code is the same, while example 1 has better (more exact) line numbers if an exception occurs.
– Caramiriel
Mar 27 at 14:08
Generated code is the same, while example 1 has better (more exact) line numbers if an exception occurs.
– Caramiriel
Mar 27 at 14:08
This is a compiler feature, not a run-time feature
– Erno de Weerd
Mar 27 at 14:09
This is a compiler feature, not a run-time feature
– Erno de Weerd
Mar 27 at 14:09
@Caramiriel I forgot to mention in my orig post but that was my counter argument too.
– Skadoosh
Mar 27 at 14:09
@Caramiriel I forgot to mention in my orig post but that was my counter argument too.
– Skadoosh
Mar 27 at 14:09
1
1
Just go to the C# spec. 12.7.11.3 Object initializers: "A member initializer that specifies an expression after the equals sign is processed in the same way as an assignment (§12.18.2) to the field or property."
– Raymond Chen
Mar 27 at 14:12
Just go to the C# spec. 12.7.11.3 Object initializers: "A member initializer that specifies an expression after the equals sign is processed in the same way as an assignment (§12.18.2) to the field or property."
– Raymond Chen
Mar 27 at 14:12
|
show 1 more comment
0
active
oldest
votes
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.
2
They're literally the same thing. Check the IL.
– Amy
Mar 27 at 14:08
Generated code is the same, while example 1 has better (more exact) line numbers if an exception occurs.
– Caramiriel
Mar 27 at 14:08
This is a compiler feature, not a run-time feature
– Erno de Weerd
Mar 27 at 14:09
@Caramiriel I forgot to mention in my orig post but that was my counter argument too.
– Skadoosh
Mar 27 at 14:09
1
Just go to the C# spec. 12.7.11.3 Object initializers: "A member initializer that specifies an expression after the equals sign is processed in the same way as an assignment (§12.18.2) to the field or property."
– Raymond Chen
Mar 27 at 14:12