只读变量VS只读属性

Ash*_*pta 8 c# oop

public static string BoldStartTag { get { return "<B>"; } }
Run Code Online (Sandbox Code Playgroud)

VS

   public static readonly string BoldStartTag  = "<B>"; 
Run Code Online (Sandbox Code Playgroud)

要么

public const string BoldStartTag  = "<B>"; 
Run Code Online (Sandbox Code Playgroud)

哪个是首选?我会认为readonly/constant变量因为我没有在属性中进行任何计算(只是返回).此外,C#编译器将弹出readonly属性的方法,而readonly变量将只是IL中的变量.

你的意见?

Dav*_*und 9

Jeff Atwood不久前写了一篇关于Properties vs Public Variables的文章.

我认为这里要考虑的一些最有趣的观点是他在更新中提到的:

  • 反射在变量与属性上的工作方式不同,因此如果依赖于反射,则更容易使用所有属性.
  • 您无法对变量进行数据绑定.
  • 将变量更改为属性是一个重大变化.