When creating an instance and assigning values to its properties, you generally have two common options.
The first is the traditional approach, where you create the object and then assign each property individually:
var person = new Person("dotnetdave@live.com", "382710933");
person.FirstName = "David";
person.LastName = "McCarter";
person.BornOn = DateTime.UtcNow;
person.Phone = "555-555-5555";
Since C# 3.0 and .NET Framework 3.5, released in November 2007, you can also use object initializers. This syntax combines object construction and property assignment into a single statement, often making the code easier to scan:
var person = new Person("dotnetdave@live.com", "382710933")
{
FirstName = "David",
LastName = "McCarter",
BornOn = DateTime.UtcNow,
Phone = "555-555-5555"
};
Now, let’s compare the performance of these two approaches.
Performance Breakdown
In my benchmark, the traditional approach was approximately 1.02× faster than using an object initializer.
That difference is small, but it is measurable. However, it should not be treated as a universal rule. The generated machine code can vary depending on the .NET version, JIT optimizations, the type being initialized, property setters, and the surrounding code.
Also, Person is a class, both examples allocate memory for the Person instance itself. The relevant result is that the object initializer did introduce additional managed allocations in this benchmark.
Object initializers primarily exist to improve readability and reduce repetitive assignment code. In most application code, that clarity is more valuable than a performance difference this small.
For code that runs in a hot path, such as a tight loop or high-throughput processing pipeline, benchmark both approaches in the actual workload. Use the traditional assignment style only when measurements show that the small improvement is meaningful for the application.
Recommendation: Prefer object initializers for clear, maintainable code. Consider traditional property assignments only in performance-critical paths where benchmarking demonstrates a measurable benefit.


Pick up any books by David McCarter by going to Amazon.com: http://bit.ly/RockYourCodeBooks
Make a one-time donation
Make a monthly donation
Make a yearly donation
Choose an amount
Or enter a custom amount
Your contribution is appreciated.
Your contribution is appreciated.
Your contribution is appreciated.
If you liked this article, please buy David a cup of Coffee by going here: https://www.buymeacoffee.com/dotnetdave
© The information in this article is copywritten and cannot be reproduced in any way without express permission from David McCarter.
Discover more from dotNetTips.com
Subscribe to get the latest posts sent to your email.
