C# 设置文件:为什么我必须使用 Settings.Default?
我只是想知道为什么它是 Settings.Default.
而不是 Settings.
?
I'm just wondering why it's Settings.Default.<mysetting>
instead of just Settings.<mysetting>
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
简单地说:因为
Settings
是一个类,而属性是实例属性。所以你需要一个实例,创建实例的默认方式是通过Default
属性。明显的后续问题是为什么属性一开始就不只是静态的……我猜答案是,能够以不同于默认设置的方式创建设置很有用加载/保存方法...例如,从数据库或不同的文件路径加载它们。
Simply put: because
Settings
is a class, and the properties are instance properties. So you need an instance, and the default way of creating an instance is through theDefault
property.The obvious followup question is why the properties aren't just static to start with... and I surmise that the answer is that it's useful to be able to create settings in ways other than with the default settings load/save approach... for example, loading them from a database, or from a different file path.
当 Settings.CompanyName 可以创建为返回实例值的属性时,使用 Settings.Default.CompanyName 是没有意义的。
It doesn't make sense to use Settings.Default.CompanyName when Settings.CompanyName can be created as Property returning the instance's value.