[C#] ์ธ๋ฑ์„œ(Indexer)

2022. 9. 3. 20:52ยท๐Ÿ“ Language/โœ C#

์ธ๋ฑ์„œ(Indexer)

์ธ๋ฑ์„œ๋Š” ์ธ๋ฑ์Šค([])๋ฅผ ์ด์šฉํ•ด์„œ ๊ฐ์ฒด ๋‚ด์˜ ๋ฐ์ดํ„ฐ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ํ”„๋กœํผํ‹ฐ์ด๋‹ค. ๋งˆ์น˜ ๊ฐ์ฒด๋ฅผ ๋ฐฐ์—ด์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

1. ๊ตฌํ˜„

  • ํŠน๋ณ„ํ•œ ๋ฌธ๋ฒ•์ธ this []๋ฅผ ์จ์„œ ํด๋ž˜์Šค ํ”„๋กœํผํ‹ฐ์ฒ˜๋Ÿผ get๊ณผ set์„ ์ •์˜ํ•œ๋‹ค.
  • ์ž…๋ ฅ ํŒŒ๋ผ๋ฏธํ„ฐ์ธ ์ธ๋ฑ์Šค๋Š” ์—ฌ๋Ÿฌ ๋ฐ์ดํ„ฐ ํƒ€์ž…์œผ๋กœ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ฃผ๋กœ int๋‚˜ string ํƒ€์ž…์„ ์‚ฌ์šฉํ•œ๋‹ค.
class ํด๋ž˜์Šค์ด๋ฆ„
{
    ํ•œ์ •์ž ๋ฐ˜ํ™˜ํƒ€์ž… this[์ž…๋ ฅํƒ€์ž… ์ž…๋ ฅ์ด๋ฆ„]
    {
        get
        {
            // ๋‚ด๋ถ€ ๋ฐ์ดํ„ฐ ๋ฐ˜ํ™˜
        }
        set
        {
            // ๋‚ด๋ถ€ ๋ฐ์ดํ„ฐ ์ €์žฅ
        }
    }
}

 

2. ํ™œ์šฉ

1) intํ˜• ์ธ๋ฑ์Šค๋กœ ๋ฐฐ์—ด ์ ‘๊ทผ

class MyClass
{
    private const int MAX = 10;
    private string name;
    private int[] data = new int[MAX];

    // ์ธ๋ฑ์„œ ์ •์˜
    public int this[int index]
    {
       get
       {
          if (index < 0 || index >= MAX)
          {
             throw new IndexOutOfRangeException();
          }
          else
          {
              return data[index];
           }
        }
        set
        {
           if (!(index < 0 || index >= MAX))
           {
               data[index] = value;
            }
         }
    }
}

class Program
{
    static void Main(string[] args)
    {
        MyClass cls = new MyClass();

        // ์ธ๋ฑ์„œ set ์‚ฌ์šฉ
        cls[1] = 1024;

        // ์ธ๋ฑ์„œ get ์‚ฌ์šฉ
        int i = cls[1];
    }
}

 

2) intํ˜• ์ธ๋ฑ์Šค๋กœ ๋ณ€์ˆ˜ ์ ‘๊ทผ

public class Person
{
    public string Name { get; set; }
    public string Gender { get; set; }
    public int Age { get; set; }
    public string Email { get; set; }

    public Object this [int index]
    {
        get
        {
            if (index == 0)
                return Name;
            else if (index == 1)
                return Gender;
            else if (index == 2)
                return Age;
            else if (index == 3)
                return Email;
            else
                return null;
        }
        set
        {
            if (index == 0)
                Name = value.ToString();
            else if (index == 1)
                Gender = value.ToString();
            else if (index == 2)
                Age = Convert.ToInt32(value);
            else if (index == 3)
                Email = value.ToString();
        }
    }

    public override string ToString()
    {
        return "Name: " + Name + " / Gender: " + Gender + " / Age: " + Age + " / Email: " + Email;
    }
}

class Program
{
    static void Main(string[] args)
    {
        Person person = new Person() {
            Name="Tom", 
            Gender="Male", 
            Age= 20, 
            Email="Tom@Test.com" 
        };

        Console.WriteLine("Name: " + person[0]);
        Console.WriteLine("Gender: " + person[1]);
        Console.WriteLine("Age: " + person[2]);
        Console.WriteLine("Email: " + person[3]);
    }
}

 

3) stringํ˜• ์ธ๋ฑ์Šค๋กœ ๋ณ€์ˆ˜ ์ ‘๊ทผ

public class Person
{
    public string Name { get; set; }
    public string Gender { get; set; }
    public int Age { get; set; }
    public string Email { get; set; }

    public Object this [int index]
    {
        get
        {
            if (indexName.ToUpper() == "NAME")
                return Name;
            else if (indexName.ToUpper() == "GENDER")
                return Gender;
            else if (indexName.ToUpper() == "AGE")
                return Age;
            else if (indexName.ToUpper() == "EMAIL")
                return Email;
            else
                return null;
        }
        set
        {
            if (indexName.ToUpper() == "NAME")
                Name = value.ToString();
            else if (indexName.ToUpper() == "GENDER")
                Gender = value.ToString();
            else if (indexName.ToUpper() == "AGE")
                Age = Convert.ToInt32(value);
            else if (indexName.ToUpper() == "EMAIL")
                Email = value.ToString();
        }
    }

    public override string ToString()
    {
        return "Name: " + Name + " / Gender: " + Gender + " / Age: " + Age + " / Email: " + Email;
    }
}

 

์ฐธ๊ณ 

https://developer-talk.tistory.com/323

 

[C#]์ธ๋ฑ์„œ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•

์ธ๋ฑ์„œ๋ž€? ์ธ๋ฑ์„œ(Indexers)๋Š” ํด๋ž˜์Šค์˜ ๋ฉค๋ฒ„๋กœ ํด๋ž˜์Šค์˜ ๊ฐ’์„ ๋ฐฐ์—ด์ฒ˜๋Ÿผ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ฃผ๋Š” ๋ฌธ๋ฒ•์ž…๋‹ˆ๋‹ค. ์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” ์ธ๋ฑ์„œ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•์„ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค. ๋ชฉ์ฐจ ์ธ๋ฑ์„œ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”

developer-talk.tistory.com

 

์ €์ž‘์žํ‘œ์‹œ (์ƒˆ์ฐฝ์—ด๋ฆผ)
'๐Ÿ“ Language/โœ C#' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
  • [C# 6.0] ์ƒˆ๋กœ์šด ๊ธฐ๋Šฅ (1) - ํ‘œํ˜„์‹ ๋ ˆ๋ฒจ
  • [C#] Partial ํƒ€์ž…, Partial ๋ฉ”์„œ๋“œ
  • [C#] ํ”„๋กœํผํ‹ฐ(Property)
  • [C#] ref์™€ out ํ‚ค์›Œ๋“œ
Blxxming
Blxxming
CS ์ง€์‹๊ณผ ๊ณต๋ถ€ํ•˜๋‹ค ๋ฐฐ์šด ๊ฒƒ, ๊ฒฝํ—˜ํ•œ ๊ฒƒ ๋“ฑ์„ ๊ธฐ๋กํ•˜๋Š” ๋ธ”๋กœ๊ทธ์ž…๋‹ˆ๋‹ค.
  • Blxxming
    ๐Ÿ’ก๋ฒˆ๋œฉ๐Ÿ’ก
    Blxxming
  • ์ „์ฒด
    ์˜ค๋Š˜
    ์–ด์ œ
  • ๊ณต์ง€์‚ฌํ•ญ

    • Tech Interview
    • ๐Ÿ“š Tech (246)
      • ๐Ÿ“ Computer Science (96)
        • โœ OS (12)
        • โœ Network & Web (10)
        • โœ Database (11)
        • โœ Data Structure (6)
        • โœ Algorithm (40)
        • โœ Design Pattern (9)
        • โœ Cloud Computing (3)
        • โœ (5)
      • ๐Ÿ“ Language (73)
        • โœ Language (6)
        • โœ C & C++ (11)
        • โœ C# (19)
        • โœ JAVA (37)
      • ๐Ÿ“ Game (43)
        • โœ Computer Graphics (2)
        • โœ Unity (14)
        • โœ Unreal (26)
        • โœ (1)
      • ๐Ÿ“ Book (34)
        • โœ Effective (3)
        • โœ Game Server (16)
        • โœ Clean Code (14)
        • โœ (1)
  • hELLOยท Designed By์ •์ƒ์šฐ.v4.10.0
Blxxming
[C#] ์ธ๋ฑ์„œ(Indexer)
์ƒ๋‹จ์œผ๋กœ

ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”