1

プロパティには次の形式があることを知っています。

class MyClass
{
    public int myProperty { get; set; }
}

これにより、次のことが可能になります。

MyClass myClass = new MyClass();
myClass.myProperty = 5;
Console.WriteLine(myClass.myProperty); // 5

ただし、次のクラスになるようにするにはどうすればよいですか。

class MyOtherClass
{
    public int[,] myProperty
    {
        get
        {
            // Code here.
        }
        set
        {
            // Code here.
        }
    }
}

次のように動作します。

/* Assume that myProperty has been initialized to the following matrix:

myProperty = 1 2 3
             4 5 6
             7 8 9

and that the access order is [row, column]. */

myOtherClass.myProperty[1, 2] = 0;

/* myProperty = 1 2 3
                4 5 0
                7 8 9 */

Console.WriteLine(myOtherClass.myProperty[2, 0]); // 7

前もって感謝します!

4

5 に答える 5

5

プロパティゲッターを公開して使用することができます:

class MyOtherClass
{
    public MyOtherClass()
    {
       myProperty = new int[3, 3];
    }

    public int[,] myProperty
    {
        get; private set;
    }
}
于 2013-06-11T22:14:02.707 に答える
3

配列を直接公開する他の回答に加えて、Indexerの使用を検討できます。

public class MyIndexedProperty
{
    private int[,] Data { get; set; }

    public MyIndexedProperty()
    {
        Data = new int[10, 10];
    }

    public int this[int x, int y] {

        get
        {
            return Data[x, y];
        }
        set
        {
            Data[x, y] = value;
        }
    }

}

したがって、クラスは次のようになります。

public class IndexerClass
{

    public MyIndexedProperty IndexProperty { get; set; }

    public IndexerClass()
    {
        IndexProperty = new MyIndexedProperty();
        IndexProperty[3, 4] = 12;
    }

}

アクセスする前に Data が初期化されていることを確認する必要があることに注意してください。これはMyIndexedPropertyコンストラクターで行いました。

使用中の結果は次のとおりです。

IndexerClass indexedClass = new IndexerClass();
int someValue = indexedClass.IndexProperty[3, 4]; //returns 12

このアプローチの主な利点は、値を格納する方法の実際の実装を呼び出し元の set および get メソッドの使用から隠すことです。

set操作の続行を決定する前に値を確認することもできます。

    public int this[int x, int y] {
        get
        {
            return Data[x, y];
        }
        set
        {
            if (value > 21) //Only over 21's allowed in here
            {
                Data[x, y] = value;
            }
        }
    }
于 2013-06-11T22:18:43.697 に答える
3

自動プロパティを使用して、プロパティの実際の実装をバイパスし、コンパイラがそれを実行できるようにすることができます。

public class Test
{
    // no actual implementation of myProperty is required in this form
    public int[,] myProperty { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        Test t = new Test();
        t.myProperty = new int[,] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
        Console.WriteLine(t.myProperty[1, 2]);
        t.myProperty[1, 2] = 0;
        Console.WriteLine(t.myProperty[1, 2]);
    }
}
于 2013-06-11T22:14:38.067 に答える
1

配列の問題は、使用する前にサイズを設定する必要があることです。たとえば、次のようにします。

class MyOtherClass
{
    public MyOtherClass(int xMAx, int yMax)
    {
        MyProperty = new int[xMAx, yMax];
    }

    public int[,] MyProperty { get; private set; }
}

プロパティは set メソッドを公開する必要はありませんMyProperty。これは、設定している ではなく、 の内部値であるためですMyProperty。たとえば、MyProperty[1,3] です。

于 2013-06-11T22:16:55.357 に答える
0

配列の代わりにリストを使用できます。

public Myclass
{
int a{get;set;};
int b{get;set;};
}

.....

public List<MyClass> myList{get;set;}
于 2013-06-11T22:18:38.640 に答える