34

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?

7
  • possible duplicate of C#: Static readonly vs const
    – Cheng Chen
    Commented Aug 11, 2011 at 9:28
  • 4
    @Danny, this is not about readonly fields, this is about properties returning a constant expression. Commented Aug 11, 2011 at 9:30
  • @Albin: It does however have similar semantics and is effectively the same thing. Commented Aug 11, 2011 at 9:35
  • @Albin Sunnanbo: A property with get accessor only can be considered as readonly.
    – Cheng Chen
    Commented Aug 11, 2011 at 9: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. Commented Aug 11, 2011 at 9:37

4 Answers 4

19

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.

1
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.)

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.

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;
       }
   }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.