public static string BoldStartTag { get { return "<B>"; } }
VS
public static readonly string BoldStartTag = "<B>";
or
public 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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With