C#:公共方法{get{}集{}问题

C#:公共方法{get{}集{}问题,c#,.net,indexer,C#,.net,Indexer,我不完全确定我是否所有的术语都正确,所以如果我错了,请原谅我。我想知道是否可以向该方法发送参数。以下面的例子为例 public item (int index) { get { return list[index]; } set { list[index] = value; } } 我知道,事实上,它会出错。我想问的是是否有办法让它工作。有什么建议吗?或者我应该想办法解决这个问题吗 提前感谢。试试这个: // This returns an instance o

我不完全确定我是否所有的术语都正确,所以如果我错了,请原谅我。我想知道是否可以向该方法发送参数。以下面的例子为例

public item (int index)  
{  
    get { return list[index]; }  
    set { list[index] = value; }  
}
我知道,事实上,它会出错。我想问的是是否有办法让它工作。有什么建议吗?或者我应该想办法解决这个问题吗

提前感谢。

试试这个:

// This returns an instance of type "Foo",
// since I didn't know the type of "list".
// Obviously the return type would need to
// match the type of whatever "list" contains.
public Foo this[int index]
{
    get { return list[index]; }
    set { list[index] = value; }
}

这是C#的语法,它有一些限制(不如VB.NET的参数属性灵活),但它确实适用于您的特定示例。

这称为索引器属性

public int this [int index]  
{      
     get { return list[index]; }      
     set { list[index] = value; }  
}

我想你可能要找的是:

public Something this[int index]
{
    get
    {
         return list[index];
    }
    set
    {
         list[index] = value;
    }
}

如其他人所示,您可以将其转换为索引器—顺便说一句,它可以有多个参数


你不能用C#命名索引器。。。虽然你可以在VB中。所以你不能有两个索引器,一个叫做
Foo
,另一个叫做
Bar
。。。您需要编写返回值的属性,这些值本身是可索引的。说实话,这有点痛苦:(

你可以用指数化来解决这个问题

public object this[string name]
        {
            get
            {
                int idx = FindParam(name);
                if (idx != -1)
                    return _params[idx].Value;

                throw new IndexOutOfRangeException(String.Format("Parameter \"{0}\" not found in this collection", name));
            }
            set 
            { 
                int idx = FindParam(name);
                if (idx != -1)
                    _params[idx].Value = value;
                else
                    throw new IndexOutOfRangeException(String.Format("Parameter \"{0}\" not found in this collection", name));
            }
        }

对于记录,当其他答案是有效的时,您可能还想考虑使用以下方法:

public IList<Something> items { get; set; }
其他答案将以以下略有不同的方式使用:

Something item = myFoo[1];

您想要的索引取决于您想要实现的具体内容,如果不查看代码的其余部分,就很难确定它。

除了已经多次提到的索引器之外,另一种可能性是使用索引器创建自定义类,并将其实例作为属性返回

例如:

public class IntList
{
    public IntList(IEnumerable<int> source)
    {
        items = source.ToArray();
        Squares = new SquareList(this);
    }

    private int[] items;

    // The indexer everyone else mentioned
    public int this[int index]
    {
        get { return items[index]; }
        set { items[index] = value; }
    }

    // Other properties might be useful:
    public SquareList Squares { get; private set; }

    public class SquareList
    {
        public SquareList(IntList list)
        {
            this.list = list;
        }

        private IntList list;

        public int this[int index]
        {
            get { return list.items[index] * list.items[index]; }
        }
    }
}
公共类IntList
{
公共IntList(IEnumerable源)
{
items=source.ToArray();
正方形=新的正方形列表(此);
}
私人物品;
//其他人提到的索引器
公共int这个[int索引]
{
获取{返回项[索引];}
集合{items[index]=value;}
}
//其他属性可能有用:
公共平方列表平方{get;private set;}
公共类列表
{
公共列表(IntList列表)
{
this.list=列表;
}
私人名单;
公共int这个[int索引]
{
获取{return list.items[index]*list.items[index];}
}
}
}

Wow,这很简单。工作起来很有魅力。谢谢!而且,列表就是列表,这会改变什么吗?然后用
Color
替换
Foo
。它只会改变参数的返回类型-我编辑了我的答案来反映这一点。+1。不过,我从来都不太明白这一点-在VB中调用参数ful的语法属性看起来就像一个方法调用(当然是这样),那么它的优点是什么?它看起来和行为都像一个方法,但在程序集中有“属性”元数据?@Andrew您可以对属性使用赋值语法:Product.Description(“en-US”)=“Tea”,当然可以作为Product.SetDescription(“en-US”,“Tea”).parameterful属性的参数与一般属性的参数完全相同-它们只是方法调用,您可以将它们作为方法调用(如在Java中)等等。这是有道理的-我想唯一奇怪的是,至少C#的索引器看起来不同,这有助于可读性,如果没有其他东西的话。VB语法很容易混淆,因为它与方法无法区分。
public class IntList
{
    public IntList(IEnumerable<int> source)
    {
        items = source.ToArray();
        Squares = new SquareList(this);
    }

    private int[] items;

    // The indexer everyone else mentioned
    public int this[int index]
    {
        get { return items[index]; }
        set { items[index] = value; }
    }

    // Other properties might be useful:
    public SquareList Squares { get; private set; }

    public class SquareList
    {
        public SquareList(IntList list)
        {
            this.list = list;
        }

        private IntList list;

        public int this[int index]
        {
            get { return list.items[index] * list.items[index]; }
        }
    }
}