Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any performance benefit in using const or readonly modifiers on fields in c#?

Is there any performance benefit to using const or readonly fields compared to regular, modifiable fields, when only using private variables.

For example:

public class FooBaar
{
     private string foo = "something";
     private const string baar = "something more"

     public void Baaz()
     {
         //access foo, access baar
     }
}

In the above example you can see there are two fields: foo and baar. Both are unaccessible outside the the class, so how come many people prefer to use const here, instead of just private. Does the const provide any performance benefit?


This question was previously closed by the community, because people misunderstood this question as "What is the difference between const and readonly in terms of performance?", which has been answered here: What is the difference between const and readonly?.
But what I actually mean is, "do I get any performance benefit by using const or readonly over not using any of them".

like image 501
Rusi Nova Avatar asked Aug 03 '11 12:08

Rusi Nova


People also ask

Does using const improve performance?

const correctness can't improve performance because const_cast and mutable are in the language, and allow code to conformingly break the rules. This gets even worse in C++11, where your const data may e.g. be a pointer to a std::atomic , meaning the compiler has to respect changes made by other threads.

Why we prefer the variable element to be a const rather than readonly?

const is used to create a constant at compile time. readonly field value can be changed after declaration. const field value cannot be changed after declaration. readonly fields cannot be defined within a method.

Why would you use a const field?

The const keyword tells the compiler that it can be fully evaluated at compile time. There is a performance & memory advantage to this, but it is small. Actually, there is no performance or memory difference in the case of a local variable.

What is the difference between const and readonly field?

The first, const, is initialized during compile-time and the latter, readonly, initialized is by the latest run-time. The second difference is that readonly can only be initialized at the class-level. Another important difference is that const variables can be referenced through "ClassName.


1 Answers

A const will be optimized by the compiler to be inlined into your code, a readonly cannot be inlined. However you cannot make constants of all types - so here you must make them readonly.

So if you need a constant value in your code, you should first look to use a const if possible, if not then readonly is there to allow you to have the safety, but not the performance benefits.

As an example:

public class Example
{
    private const int foo = 5;
    private readonly Dictionary<int, string> bar = new Dictionary<int, string>();

    //.... missing stuff where bar is populated

    public void DoSomething()
    {
       Console.Writeline(bar[foo]);

       // when compiled the above line is replaced with Console.Writeline(bar[5]);
       // because at compile time the compiler can replace foo with 5
       // but it can't do anything inline with bar itself, as it is readonly
       // not a const, so cannot benefit from the optimization
    }
}
like image 179
iandotkelly Avatar answered Nov 15 '22 19:11

iandotkelly