c# - 提供对类私有成员属性的访问
问题描述
我想知道返回私有成员属性的更有效或最佳实践是什么。例如:
class Foo
{
private List<int> fooList;
public Foo()
{
Random random = new Random();
fooList = new List<int>(random.Next(1, 100));
}
//
public int Count { get { return fooList.Count; } }
// or
public int Count() { return fooList.Count; }
}
如果我不想让公众访问我的列表,哪个最好?
解决方案
根据您的示例,您应该留在房产中。因为它fooList.Count
是一个属性。
推荐阅读
- data-cleaning - 如何获取数据子集并在 r 中进行汇总
- solr - 了解 solr(8.x) 默认 searchHandler
- firebase - Firebase 云功能在 Cordova 上不起作用
- ruby-on-rails - 如何跨 2 个 rails 实例处理 CSRF 令牌?
- go - 使用 gocolly 获取 div 元素失败
- java - 如何使用一个 jar 文件在同一个应用程序中运行服务器和客户端框架
- azure-data-factory - 如何强制将管道的状态设置为失败
- javascript - 如何修复过滤器搜索以在列表项内显示仪表图
- python - 如何过滤从条件索引开始的自定义间隔内的时间戳?
- sql - 如何从 Node.js 将记录插入 Postgres?node-pg 没有很好的文档记录