Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Comments on C#: Performance hit from using calculated property instead of get-only property with initializer?

Post

C#: Performance hit from using calculated property instead of get-only property with initializer?

+5
−0

JetBrains Rider suggests that I change this (for example):

public class Foo {
    public int OnePlusOne { get; } = 1 + 1;
}

to this:

public class Foo {
    public int OnePlusOne => 1 + 1;
}

As I understand it, the first example runs the calculation once and stores the result, whereas the second recalculates it every time. In examples where the calculation is more complex, won't the first have better performance than the second?

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

Have you tested your assumption? (5 comments)
Have you tested your assumption?
Michael‭ wrote 16 days ago · edited 16 days ago

Try it with dynamic values and see what happens. Then you can answer your own question.

public long TimeStamp1 { get; } = DateTime.Now.Ticks;
public long TimeStamp2 => DateTime.Now.Ticks;

Console.Log(Foo.TimeStamp1);
Console.Log(Foo.TimeStamp2);
Thread.Sleep(3000);
Console.Log(Foo.TimeStamp1);
Console.Log(Foo.TimeStamp2);
Thread.Sleep(3000);
Console.Log(Foo.TimeStamp1);
Console.Log(Foo.TimeStamp2);
Michael‭ wrote 16 days ago

My personal suspicion is that ReSharper thinks most people meant to have dynamic resolution of the property getter. ReSharper would then expect people to be surprised to discover that the initial value is never changed. (I can submit this as an answer if that's useful.)

Alexei‭ wrote 15 days ago

I think Michael is right. Getters are just syntactical sugar that get transformed into parameterless functions. In most cases the caller expects for this to evaluate based on object's state and not "cache" the initial value.

If you want to store the initial evaluation result, define a static readonly field instead to better convey the meaning (it is not supposed to change).

Dave‭ wrote 10 days ago

Pretty sure OP's understanding is correct. { get; } = 1 + 2; will compute once with the value stored in a hidden backing field. (For a non-static class/property it would be computed once for each instance).

=> 1 + 2; will evaluate each time (possibly save for optimizations).

What I wonder though is why Rider is suggesting this. If I write this code I have no such suggestion. There is a refactoring available to convert it to the arrow style, but it is specifically labelled "To computed property (evaluate each read)".

If Rider really is suggesting this it may be that you have set somewhere a preference for the arrow style; but I'm not sure about that. I think it may be that your real example differs in an important way and that is why the suggestion is being made, but that's just a guess.

Michael‭ I don't care about performance per se, but about understanding the language. I should have worded my question differently. Confirmation that a get-only property and a calculated property are different answers my question. Many thanks!