【发布时间】:2011-01-08 17:42:38
【问题描述】:
我是 C# 世界的新手,我找不到在 C# 中声明只读变量的方法(类似于在 C++ 中声明“const”变量)。有吗?
我给你举个例子:
...
int f() { return x; } // x is not const member
...
void g() {
int readOnlyVar = f(); // is there a method to declare readOnlyVar as read only or const
// Some code in which I want to restrict access to readOnlyVar to read only
}
【问题讨论】:
-
老兄,我认为遵循
referential transparency指南就足够了,您的methods must be as small让您一眼就可以知道应该做什么.