인덱서 예제

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System;
 
namespace IndexerEx
{    
    class ClassA
    {
        private int[] array;
        
        public ClassA()
        {
            array = new int[1];
        }
        
        public int this[int index]
        {
            get { return array[index]; }
            
            set
            {
                if (index >= array.Length)
                {
                    Array.Resize<int>(ref array, index + 1);
                    Console.WriteLine("Array Resized: {0}", array.Length);
                }
                
                array[index] = value;
            }
        }
        
        public int Length
        {
            get { return array.Length; }
        }
    }
    class MainClass
    {
        public static void Main (string[] args)
        {
            ClassA a = new ClassA();
            
            for (int i = 0; i < 10++i)
            {
                a[i] = i;
            }
            
            for (int j = 0; j < a.Length; ++j)
            {
                Console.WriteLine(a[j]);
            }
            
            Console.ReadKey();
        }
    }
}
 
cs


+ Recent posts