是否可以将参数传递给属性?

时间:2017-07-14 20:20:02

标签: c# wpf

如果我有这个属性:

public ListView GetCurrentListView

我可以传递

等参数
public ListView GetCurrentListView(bool flag)

2 个答案:

答案 0 :(得分:1)

你的第二个例子不再是属性,而是一种方法。

您不能拥有同名的方法和属性。您通常应该做其中一个,但如果您同时需要属性和方法,则需要为每个属性使用不同的名称。

答案 1 :(得分:1)

只有选项是indexer属性,您可以将属性的类型指定为委托:

void Main()
{
    var sample = new Sample();
    var odd = sample.Numbers(true);
}
class Sample
{
    public Func<bool, IEnumerable<int>> Numbers 
    { 
        get => b => Enumerable.Range(1, 100).Select(x => b ? x * 2 : x * 2 - 1); 
    }
}