提供对类私有成员属性的访问

时间:2018-11-21 15:46:45

标签: 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; }
}

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

1 个答案:

答案 0 :(得分:0)

根据您的示例,您应该保留财产。因为fooList.Count是一个属性。

相关问题