5

すべての用語が正しいかどうかは完全にはわかりません。間違っている場合はご容赦ください。メソッドに引数を送信できるかどうか疑問に思いました。たとえば、次のようにします。

public item (int index)  
{  
    get { return list[index]; }  
    set { list[index] = value; }  
}

そのままではエラーになることは承知しています。私が求めているのは、それを機能させる方法があるかどうかです。何か提案がありますか、それとも私はそれを回避する方法を理解する必要がありますか?

前もって感謝します。

4

7 に答える 7

21

これを試して:

// 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のパラメーター付きプロパティほど柔軟ではありません)が、特定の例では機能します。

于 2009-09-24T13:11:50.000 に答える
6

他の人が示しているように、それをインデクサーに変えることができます-ちなみに、複数のパラメーターを持つことができます。

あなたができないことは、C#でインデクサーに名前を付けることです...VBではできますが。したがって、2つのインデクサーを作成することはできません。1つは呼び出されFoo、もう1つは呼び出されBarます...それ自体がインデックス可能な値を返すプロパティを作成する必要があります。正直言って、少し苦痛です:(

于 2009-09-24T13:14:12.477 に答える
5

これはインデクサープロパティと呼ばれます

public int this [int index]  
{      
     get { return list[index]; }      
     set { list[index] = value; }  
}
于 2009-09-24T13:13:37.797 に答える
3

私はあなたが探しているかもしれないものは次のとおりだと思います:

public Something this[int index]
{
    get
    {
         return list[index];
    }
    set
    {
         list[index] = value;
    }
}
于 2009-09-24T13:13:43.537 に答える
1

記録として、他の回答は有効ですが、次のアプローチの使用を検討することもできます。

public IList<Something> items { get; set; }

これは、次のように使用できます。

Something item = myFoo.items[1];

他の回答は、少し異なる方法で次のように使用されます。

Something item = myFoo[1];

必要なものは、正確に何を達成しようとしているのかによって異なります。これは、残りのコードを見ずに判断するのは困難です。

于 2009-09-24T13:20:13.890 に答える
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]; }
        }
    }
}
于 2009-09-24T13:24:19.550 に答える
0

この問題を解決するためにインデクサーを使用できます

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));
            }
        }
于 2009-09-24T13:14:46.937 に答える