如果我在课堂上有私人财产,我想知道在以下三种情况(内存使用,可用性,最佳实践等)之间的区别是什么:
class testClass
{
private string myString1 = "hello";
private string myString2 { get { return "hello"; } }
private string myString3() { return "hello"; }
}
Run Code Online (Sandbox Code Playgroud)
除了显然能够设置myString1中的值而不是myString2或myString3中的值之外,我想知道更多关于它们在效率方面有何不同?
我个人更喜欢没有副作用的物品和明确的吸气剂,如果有动物计算的话.例如:
class User {
private string username;
public string Username {
get { return username; }
set { username = value; }
}
public Post GetLatestPost() {
// query the database or whatever you do here.
}
}
Run Code Online (Sandbox Code Playgroud)
我见过的很多API看起来都差不多.希望有所帮助.
| 归档时间: |
|
| 查看次数: |
3459 次 |
| 最近记录: |