General Performance: Expanded & Expression Bodied Methods

A contemporary approach to crafting concise methods in .NET involves using expression-bodied methods. The conventional or expanded method appears as follows:

public Person CreatePerson()
{
    return base.CreatePerson<Person>();
}

Converting this method to an expression-bodied method results in the following:

public Person CreatePerson() => base.CreatePerson<Person>();

I prefer expanded methods for their readability. I was curious to determine if utilizing expression-bodied methods would result in better performance.

Performance Breakdown

The benchmark results show that the performance of expression-bodied methods is 1.4x less performant than that of regular methods.

Prefer a regular method for better readability, unless the expression-bodied method is very short and enhances clarity. In most cases, readability and maintainability should take precedence over style.

Always benchmark any changes using tools like BenchmarkDotNet to ensure your optimizations result in measurable improvements in your actual codebase.

To analyze your code using the same settings I used in these articles, I encourage you to incorporate my EditorConfig file. It can be found at the following link: https://bit.ly/dotNetDaveEditorConfig.

Please feel free to leave a comment below. I would appreciate hearing your thoughts and feedback.

Pick up any books by David McCarter by going to Amazon.com: http://bit.ly/RockYourCodeBooks

One-Time
Monthly
Yearly

Make a one-time donation

Make a monthly donation

Make a yearly donation

Choose an amount

$5.00
$15.00
$100.00
$5.00
$15.00
$100.00
$5.00
$15.00
$100.00

Or enter a custom amount

$

Your contribution is appreciated.

Your contribution is appreciated.

Your contribution is appreciated.

DonateDonate monthlyDonate yearly

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.

Leave a Reply