07
23

C# 인덱서 정의

C# Indexer는 특별한 문법인 this[]를 써서 Property처럼 get과 set을 정의한다. 클래스 내부의 어떤 데이터를 리턴하는지는 클래스 디자인 시 필요에 따라 정하게 될 것이며, 리턴 데이터 타입도 여러 가지로 지정할 수 있다.

입력 파라미터인 인덱스도 여러 데이터 타입을 쓸 수 있으며, 주로 int, string 타입을 사용하여 인덱스 값을 준다.

 

ex)

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp5
{
    class TestInt 
    {
        private int[] _array = new int[6];// 인덱서 indexer

        public int this[int Index]
        {
            get { return _array[Index]; }
            set { _array[Index] = value; }
        }
    }

    class Test
    {
        private string[] _array = new string[5];

        public string this[int Index]
        {
            get { return _array[Index]; }
            set { _array[Index] = value; }
        }
    }
    class IndexerTest
    {
        
        private Hashtable myFavorite = new Hashtable();
        public string this[string kind]
        {
            get { return (string)myFavorite[kind]; }
            set { myFavorite[kind] = value; }
        }

       

        static void Main(string[] args)
        {

            IndexerTest it = new IndexerTest();
            it["fruit"] = "apple";
            it["color"] = "blue";
            Console.WriteLine(it["fruit"]);
            Console.WriteLine(it["color"]);

            Test test = new Test();
            test[0] = "test";

            Test atest = new Test();
            atest[0] = "할룽a";
            atest[1] = "할룽b";
            atest[2] = "할룽c";
            atest[3] = "할룽d";
            atest[4] = "할룽e";
            Console.WriteLine(atest[2]);

            TestInt testInt = new TestInt();
            testInt[0] = 0;
            testInt[1] = 1;
            testInt[2] = 2;
            testInt[3] = 3;
            testInt[4] = 4;
            testInt[5] = 5;
            Console.WriteLine(testInt[4]);
        }
    }
}

 

예제 실행 화면

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class TestInt
{
    private int[] _array;// 인덱서 indexer

    public TestInt(int Size)
    {
        _array = new int[Size];
    }

    public int this[int Index]
    {
        get { return _array[Index]; }
        set { _array[Index] = value; }
    }
}

class Program
{
    static void Main(string[] args)
    {
        TestInt testInt1 = new TestInt(10);
        TestInt testInt2 = new TestInt(20);
    }
}

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _20200722_002
{
    class Car
    {
        public string name;
    }

    class TestCar
    {
        private Car[] _array = new Car[5];// 인덱서 indexer

        public Car this[int Index]
        {
            get { return _array[Index]; }
            set { _array[Index] = value; }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            TestCar testCar = new TestCar();
            testCar[0] = new Car();
            testCar[0].name = "test";
            testCar[1] = new Car();
            testCar[1].name = "test2";
            testCar[2] = new Car();
            testCar[2].name = "test3";

            Console.WriteLine(testCar[0].name);
        }
    }
}

 

예제 실행 화면

'스마트팩토리 > C#' 카테고리의 다른 글

25. MessageMap  (0) 2020.07.24
24. 복습  (0) 2020.07.24
22. 복습  (0) 2020.07.20
21. 게임제작, 복습  (0) 2020.07.17
20. 게임제작, 복습  (0) 2020.07.15
COMMENT