首页 > 解决方案 > 提供对类私有成员属性的访问

问题描述

我想知道返回私有成员属性的更有效或最佳实践是什么。例如:

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; }
}

如果我不想让公众访问我的列表,哪个最好?

标签: c#

解决方案


根据您的示例,您应该留在房产中。因为它fooList.Count是一个属性。


推荐阅读