33

What's the difference between the first and second definitions?

//1
private static string Mask
{
   get { return "some text"; }
}

//2 
private const string Mask  = "some text"; 

Which benefits has the first and the second approach?

Alexandre
  • 13,030
  • 35
  • 114
  • 173
  • possible duplicate of [C#: Static readonly vs const](http://stackoverflow.com/questions/755685/c-static-readonly-vs-const) – Cheng Chen Aug 11 '11 at 09:28
  • 4
    @Danny, this is not about readonly fields, this is about properties returning a constant expression. – Albin Sunnanbo Aug 11 '11 at 09:30
  • @Albin: It does however have similar semantics and is effectively the same thing. – Jeff Mercado Aug 11 '11 at 09:35
  • @Albin Sunnanbo: A property with `get` accessor only can be *considered* as readonly. – Cheng Chen Aug 11 '11 at 09:35
  • 7
    No, `readonly` means that the object can not be replaced once initializers and constuctors are done. A `get` property can return different objects every time called. – Albin Sunnanbo Aug 11 '11 at 09:37
  • On the surface it sounds like a duplicate, but it is in fact not, but to be honest I would much rather have that duplicate edited to be *the one authority on the subject* rather than fragmenting this. However, a property that returns the same value every time is not the same as a readonly field or a const. – Lasse V. Karlsen Aug 11 '11 at 09:37
  • @Albin I think the point here was that the property does in fact *not* return different values every time it is called, but rather returns the same value every time. – Lasse V. Karlsen Aug 11 '11 at 09:38

4 Answers4

18

As long as they are private they will probably be optimized to more or less the same code. The re is another story if they are public and used from other assemblies.

const variables will be substitued/inlined in other assemblies using the const expression. That means that you need to recompile every assembly using the const expression if you change the expression. On the other hand the property solution will give you a method call overhead each time used.

Albin Sunnanbo
  • 46,430
  • 8
  • 69
  • 108
4

Basically const fields value evaluated at compile time and initiailized at declaration only. Also important point that they are stored in the assembly metadata so there is could be a problem when you distributing assembly across the customers and then give them an updated version, so they need to recompile assemblies which referencing const to pick up an updated value as well.

In few words static field is something like a global variable which is accessible without instantiating any instance of underlying type, but in your case private access modifieds makes it unaccessible out of the type where it declared.

EDIT:

Very good Community Wiki post regarding constants: Referencing constants across assemblies (post no longer exists as of June 2013.)

sll
  • 61,540
  • 22
  • 104
  • 156
0

const is a keyword of the language. 1s defined, your not allowed to change it down the line. Yes the property also depicts the same story, but it can have much more structure in it.

SwissCodeMen
  • 4,222
  • 8
  • 24
  • 34
Zenwalker
  • 1,883
  • 1
  • 14
  • 27
0

In const, Once initialize can not modify. get can also not modify. But when you have a scenario where you need different values every time you call Or, your value depends on any other function call, get will be used.


private static string Mask
   {
       get
       {
           var someOtherMethodText = SomeOtherMethodText();
           return "some text" + someOtherMethodText;
       }
   }