我想要一个常量字符串kURL =“http://www.myurl.com/”;在整个项目中.在
Windows Phone 7应用程序中执行此操作的正确方法是什么?
我将把这段代码放在哪里,正确的语法应该是什么?
解决方法
为您可能需要从解决方案中的所有项目(如常量,扩展方法,实用程序等)访问的内容创建.Common项目,在那里只需创建具有您可能需要的任何常量的Constants类.
像这样:
public static class Constants { #region Nested type: Urls public static class Urls { public static readonly string MyUrl = "http://blablabla.com"; } #endregion
}
用法是:
Constants.Urls.MyUrl
祝好运.
编辑注意:根据Gabes建议更改为const
编辑注2:根据lukas建议更改为静态只读