Csharp-generics

提供:Dev Guides
移動先:案内検索

C#-ジェネリック

*Generics* を使用すると、プログラムで実際に使用されるまで、クラスまたはメソッドのプログラミング要素のデータ型の仕様を定義できます。 つまり、ジェネリックを使用すると、任意のデータ型で機能するクラスまたはメソッドを作成できます。

データ型の代替パラメータを使用して、クラスまたはメソッドの仕様を記述します。 コンパイラは、クラスのコンストラクターまたはメソッドの関数呼び出しを検出すると、特定のデータ型を処理するコードを生成します。 簡単な例は、概念を理解するのに役立ちます-

using System;
using System.Collections.Generic;

namespace GenericApplication {
   public class MyGenericArray<T> {
      private T[] array;

      public MyGenericArray(int size) {
         array = new T[size + 1];
      }
      public T getItem(int index) {
         return array[index];
      }
      public void setItem(int index, T value) {
         array[index] = value;
      }
   }
   class Tester {
      static void Main(string[] args) {

        //declaring an int array
         MyGenericArray<int> intArray = new MyGenericArray<int>(5);

        //setting values
         for (int c = 0; c < 5; c++) {
            intArray.setItem(c, c*5);
         }

        //retrieving the values
         for (int c = 0; c < 5; c++) {
            Console.Write(intArray.getItem(c) + " ");
         }

         Console.WriteLine();

        //declaring a character array
         MyGenericArray<char> charArray = new MyGenericArray<char>(5);

        //setting values
         for (int c = 0; c < 5; c++) {
            charArray.setItem(c, (char)(c+97));
         }

        //retrieving the values
         for (int c = 0; c< 5; c++) {
            Console.Write(charArray.getItem(c) + " ");
         }
         Console.WriteLine();

         Console.ReadKey();
      }
   }
}

上記のコードをコンパイルして実行すると、次の結果が生成されます-

0 5 10 15 20
a b c d e

ジェネリックの機能

ジェネリックは、次の方法であなたのプログラムを豊かにする技術です-

  • コードの再利用、型の安全性、パフォーマンスを最大化するのに役立ちます。
  • ジェネリックコレクションクラスを作成できます。 .NET Frameworkクラスライブラリには、_System.Collections.Generic_名前空間にいくつかの新しいジェネリックコレクションクラスが含まれています。 _System.Collections_名前空間のコレクションクラスの代わりに、これらの汎用コレクションクラスを使用できます。
  • 独自の汎用インターフェイス、クラス、メソッド、イベント、デリゲートを作成できます。
  • 特定のデータ型のメソッドにアクセスできるように制限された汎用クラスを作成できます。 *リフレクションを使用して、実行時に汎用データ型で使用される型に関する情報を取得できます。

ジェネリックメソッド

前の例では、ジェネリッククラスを使用しました。型パラメーターを使用してジェネリックメソッドを宣言できます。 次のプログラムは、概念を示しています-

using System;
using System.Collections.Generic;

namespace GenericMethodAppl {
   class Program {
      static void Swap<T>(ref T lhs, ref T rhs) {
         T temp;
         temp = lhs;
         lhs = rhs;
         rhs = temp;
      }
      static void Main(string[] args) {
         int a, b;
         char c, d;
         a = 10;
         b = 20;
         c = 'I';
         d = 'V';

        //display values before swap:
         Console.WriteLine("Int values before calling swap:");
         Console.WriteLine("a = {0}, b = {1}", a, b);
         Console.WriteLine("Char values before calling swap:");
         Console.WriteLine("c = {0}, d = {1}", c, d);

        //call swap
         Swap<int>(ref a, ref b);
         Swap<char>(ref c, ref d);

        //display values after swap:
         Console.WriteLine("Int values after calling swap:");
         Console.WriteLine("a = {0}, b = {1}", a, b);
         Console.WriteLine("Char values after calling swap:");
         Console.WriteLine("c = {0}, d = {1}", c, d);

         Console.ReadKey();
      }
   }
}

上記のコードをコンパイルして実行すると、次の結果が生成されます-

Int values before calling swap:
a = 10, b = 20
Char values before calling swap:
c = I, d = V
Int values after calling swap:
a = 20, b = 10
Char values after calling swap:
c = V, d = I

ジェネリックデリゲート

型パラメーターを使用して汎用デリゲートを定義できます。 たとえば-

delegate T NumberChanger<T>(T n);

次の例は、このデリゲートの使用を示しています-

using System;
using System.Collections.Generic;

delegate T NumberChanger<T>(T n);
namespace GenericDelegateAppl {
   class TestDelegate {
      static int num = 10;

      public static int AddNum(int p) {
         num += p;
         return num;
      }
      public static int MultNum(int q) {
         num* = q;
         return num;
      }
      public static int getNum() {
         return num;
      }
      static void Main(string[] args) {
        //create delegate instances
         NumberChanger<int> nc1 = new NumberChanger<int>(AddNum);
         NumberChanger<int> nc2 = new NumberChanger<int>(MultNum);

        //calling the methods using the delegate objects
         nc1(25);
         Console.WriteLine("Value of Num: {0}", getNum());

         nc2(5);
         Console.WriteLine("Value of Num: {0}", getNum());
         Console.ReadKey();
      }
   }
}

上記のコードをコンパイルして実行すると、次の結果が生成されます-

Value of Num: 35
Value of Num: 175