[C#기초] 4. 속성과 인덱서

INDEX

클래스 배열의 변수와 속성 비교

속성 사용시 예제

class A
{
   int number;
   public int Number
   {
       get { return number; }
       set { number = value; }
   }
}
class Program
{
   static void Main(string[] args)
   {
       A[] test = new A[3];
       test[0] = new A();
       test[0].Number = 12;
   }
}

속성 사용 안할 시 예제

class A
{
	public int number;
}
class Program
{
    static void Main(string[] args)
    {            
	A[] test = new A[3];
	test[0] = new A();
	A[0].number = 12;
    }
}

클래스 멤버 배열과 인덱서 형식

인덱서 특징

인덱서 예제 코드

class A
{
    int[] number = new int[3];
    public int this[int index]
    {
        get { return number[index]; }
        set { number[index] = value; }
    }
}

class Program
{
    static void Main(string[] args)
    {
        A[] test = new A[2];
        test[0] = new A();
        test[0][0] = 1;
        Console.WriteLine(test[0][0]);
    }
}

this

this