방법: C# 인덱서 사용(C++/CLI)

Visual C++에는 인덱서가 포함되어 있지 않으며, 인덱싱된 속성이 있습니다. C# 인덱서를 사용하려면 인덱싱된 속성과 마찬가지로 인덱서에 액세스합니다.

인덱서에 대한 자세한 내용은 다음 항목을 참조하십시오.

예제

다음 C# 프로그램에서는 인덱서를 정의합니다.

// consume_cs_indexers.cs
// compile with: /target:library
using System;
public class IndexerClass {
   private int [] myArray = new int[100]; 
   public int this [int index] {   // Indexer declaration
      get {
         // Check the index limits.
         if (index < 0 || index >= 100)
            return 0;
         else
            return myArray[index];
      }
      set {
         if (!(index < 0 || index >= 100))
            myArray[index] = value;
      }
   }
}
/*
// code to consume the indexer
public class MainClass {
   public static void Main() {
      IndexerClass b = new IndexerClass();

      // Call indexer to initialize elements 3 and 5
      b[3] = 256;
      b[5] = 1024;
      for (int i = 0 ; i <= 10 ; i++) 
         Console.WriteLine("Element #{0} = {1}", i, b[i]);
   }
}
*/

다음 Visual C++ 프로그램에서는 인덱서를 사용합니다.

// consume_cs_indexers_2.cpp
// compile with: /clr
#using "consume_cs_indexers.dll"
using namespace System;

int main() {
   IndexerClass ^ ic = gcnew IndexerClass;
   ic->default[0] = 21;
   for (int i = 0 ; i <= 10 ; i++)
      Console::WriteLine("Element #{0} = {1}", i, ic->default[i]);
}
  

참고 항목

기타 리소스

다른 .NET 언어와의 상호 운용성(C++/CLI)