tags:

views:

72

answers:

3
public static string BoldStartTag { get { return "<B>"; } }

VS

   public static readonly string BoldStartTag  = "<B>"; 

or

public static const string BoldStartTag  = "<B>"; 

which one is preferred? I would think the readonly/constant variable as I am not doing any computation in the property (just return). Also, the C# compiler will eject a method for the readonly property whereas the readonly variable will just be a variable in the IL.

Your thoughts?

+1  A: 

Why not use const? I would have thought that having <B> as the bold start tag would be fairly set in stone.

ho1
yeah..sure..Just wanted to have an opinion about using the read-only property in this case.
ydobonmai
+3  A: 

The preferred method for public values is always a property, for encapsulation reasons.

For your specific example, though, I'd use a const -- it's not like BoldStartTag will be changing any time soon.

zildjohn01
yeah..sure..See my edit..Just wanted to have an opinion about using the read-only property in this case.
ydobonmai
+3  A: 

Jeff Atwood wrote an article on Properties vs Public Variables a while back.

I think some of the most interesting points to consider here are the ones he mentions in his update:

  • Reflection works differently on variables vs. properties, so if you rely on reflection, it's easier to use all properties.
  • You can't databind against a variable.
  • Changing a variable to a property is a breaking change.
David Hedlund
+1 for the great link. For this specific scenario, I dont intend to do any databinding. So a constant would be good enough?
ydobonmai