-4

C#で次のことを行う方法はありますか?

List<int> aList = new List<int>();
List<int> bList = new List<int>();
... // fill the list somehow
List<int> referece; //?????
if (doThis)
    referece = aList;
else
    referece = bList;

reference= .... assign another list to reference

これはC#で可能ですか? C++ ではリストを参照しますが、C# では?


EDITED:私は私の例を修正しました。リストを新しい/異なるリストに置き換えたいと考えており、aListまたはbListを変更したいと考えています。新しいリストを参照に割り当てると、aList と bList は変更されません。しかし、それは私が本当に望んでいることです.aListまたはbListを変更してください. 参照は、リストを保持する変数を選択するだけです。

4

3 に答える 3

3

問題はどこだ?

List<int> aList = new List<int>();
List<int> bList = new List<int>();
... // fill the list somehow
List<int> referece = null;
if (doThis)
    referece = aList;
else
    referece = bList;

if(reference != null)
    reference.DoSomethingWithSelectedList();

List<T>はクラス (参照型) であるため、型の変数はすべてList<T>クラスのオブジェクトへの参照List<T>です。

于 2012-07-28T13:09:49.027 に答える
1

List<int>以下のような拡張メソッドが必要です。

 public static void DoSomethingWithSelectedList(this List<int> myList)
        {
            // your code
        }

List は c# の参照型です。

于 2012-07-28T13:16:59.250 に答える
0

Listから派生するのではなく、必要なすべてのメソッドを追加するICollectionインターフェイスを実装して、独自のリストを設計する必要があります。

于 2012-07-28T13:24:56.707 に答える