11

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".

Community
  • 1
  • 1
Rusi Nova
  • 2,617
  • 6
  • 32
  • 48
  • 3
    When you're bored with micro-optimization there's always nano-optimizing. – H H Aug 03 '11 at 13:19
  • I have edited the question and would like ppl to see it!! – Rusi Nova Aug 03 '11 at 13:36
  • I think @spender already has the perfect answer to your edited question. Don't worry about speed here, try to be correct and obvious. – H H Aug 03 '11 at 13:40
  • @Rusi Nova - your first string is not readonly it is private. To make it readonly it should read 'private readonly string foo="something"' – iandotkelly Aug 03 '11 at 13:49
  • 3
    @Henk - I (partially) disagree. What should someone do when speed is an issue? The question is about performance - the answer shouldn't just ignore that. Spender's answer is perfectly good advice, but its not an answer to this question really. – iandotkelly Aug 03 '11 at 13:52
  • 1
    If it's about performance the only sensible answer is: use a profiler to find the problem. Very unlikely to be (a missing) readonly or const. – H H Aug 03 '11 at 13:55
  • @iandotkelly: i know its private, i deliberately made it private. Thats my question: do i get any performance benefit by using const(or readonly) over just private?? when my intention is just to create a string to be shared and used intrinsically by varios methods. – Rusi Nova Aug 03 '11 at 13:59
  • 1
    - different answers - const yes, readonly no – iandotkelly Aug 03 '11 at 14:00

2 Answers2

27

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
    }
}
iandotkelly
  • 9,024
  • 8
  • 48
  • 67
-3

I wouldn't worry too much about the performance of these constructs until you encounter a critical piece of code that requires you to make such measurements. They're there to ensure correctness of code, not for performance reasons.

spender
  • 117,338
  • 33
  • 229
  • 351
  • 32
    Perfectly good advice, and I don't worry about performance either - but doesn't answer the question. – iandotkelly Aug 03 '11 at 14:29
  • 9
    This does not answer the question and assumes performance critical code is not being written. It should definitely not be the accepted answer. – Daniel Jun 23 '17 at 15:49
  • @Daniel : experience has taught me that often, when the question is "which is faster, language feature X or language feature Y", the person is prioritizing the wrong investigation. "Performance benefits" can be easily measured by the OP and don't really represent a problem that should be asked on SO. Those who come looking for the answer to this question are best reminded that there is a good chance that they are probably looking at things with the wrong mindset. – spender Jun 23 '17 at 17:05
  • 4
    That is not the question he asked, and whether or not the question should be here is not the issue. If we have a bad question, must we also have a bad answer? This answer would have been great as a comment, because that is what it is. There is an actual, more informative answer just below, gathering dust. – Daniel Jun 23 '17 at 17:32
  • @Daniel : True. Perhaps you could ping the OP to mark the other answer, then I'd happily demote this answer to a comment. Until then, "you cannot delete the accepted answer". – spender Jun 23 '17 at 17:41
  • What's done is done. It is a very old question. I was just explaining my downvote as prompted by SO. – Daniel Jun 23 '17 at 17:43