C# ジェネリック(Generic)
ジェネリック(Generic)クラスまたはメソッド内のプログラミング要素のデータ型の指定を、プログラムで実際に使用するまで遅延させることができます。言い換えれば、ジェネリックを使用すると、任意のデータ型と連携できるクラスまたはメソッドを作成できます。
データ型の代替パラメータを使用して、クラスまたはメソッドの仕様を作成できます。コンパイラがクラスのコンストラクタまたはメソッドの関数呼び出しを検出すると、指定されたデータ型を処理するコードを生成します。次の簡単な実例は、この概念を理解するのに役立ちます。
実例
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)
{
// 整数配列を宣言
MyGenericArray<int> intArray = new MyGenericArray<int>(5);
// 値を設定
for (int c = 0; c < 5; c++)
{
intArray.setItem(c, c*5);
}
// 値を取得
for (int c = 0; c < 5; c++)
{
Console.Write(intArray.getItem(c) + " ");
}
Console.WriteLine();
// 文字配列を宣言
MyGenericArray<char> charArray = new MyGenericArray<char>(5);
// 値を設定
for (int c = 0; c < 5; c++)
{
charArray.setItem(c, (char)(c+97));
}
// 値を取得
for (int c = 0; c < 5; c++)
{
Console.Write(charArray.getItem(c) + " ");
}
Console.WriteLine();
Console.ReadKey();
}
}
}
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)
{
// 整数配列を宣言
MyGenericArray<int> intArray = new MyGenericArray<int>(5);
// 値を設定
for (int c = 0; c < 5; c++)
{
intArray.setItem(c, c*5);
}
// 値を取得
for (int c = 0; c < 5; c++)
{
Console.Write(intArray.getItem(c) + " ");
}
Console.WriteLine();
// 文字配列を宣言
MyGenericArray<char> charArray = new MyGenericArray<char>(5);
// 値を設定
for (int c = 0; c < 5; c++)
{
charArray.setItem(c, (char)(c+97));
}
// 値を取得
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
ジェネリック(Generic)の特徴
ジェネリックの使用は、プログラムの機能を強化する技術であり、具体的には以下の側面に現れます:
- コードの再利用を最大限に高め、型の安全性を保護し、パフォーマンスを向上させるのに役立ちます。
- ジェネリックコレクションクラスを作成できます。.NET Frameworkクラスライブラリには、System.Collections.Generic名前空間にいくつかの新しいジェネリックコレクションクラスが含まれています。これらのジェネリックコレクションクラスを使用して、System.Collectionsのコレクションクラスの代わりに使用できます。
- 独自のジェネリックインターフェイス、ジェネリッククラス、ジェネリックメソッド、ジェネリックイベント、およびジェネリックデリゲートを作成できます。
- ジェネリッククラスに制約を設定して、特定のデータ型のメソッドにアクセスできます。
- ジェネリックデータ型で使用される型に関する情報は、実行時にリフレクションを使用して取得できます。
ジェネリック(Generic)メソッド
上記の実例では、すでにジェネリッククラスを使用しました。型パラメータを使用してジェネリックメソッドを宣言できます。次のプログラムはこの概念を示しています:
実例
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';
// 交換前に値を表示
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);
// swapを呼び出す
Swap<int>(ref a, ref b);
Swap<char>(ref c, ref d);
// 交換後に値を表示
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();
}
}
}
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';
// 交換前に値を表示
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);
// swapを呼び出す
Swap<int>(ref a, ref b);
Swap<char>(ref c, ref d);
// 交換後に値を表示
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
ジェネリック(Generic)デリゲート
型パラメータを使用してジェネリックデリゲートを定義できます。例:
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)
{
// デリゲートインスタンスを作成
NumberChanger<int> nc1 = new NumberChanger<int>(AddNum);
NumberChanger<int> nc2 = new NumberChanger<int>(MultNum);
// デリゲートオブジェクトを使用してメソッドを呼び出す
nc1(25);
Console.WriteLine("Value of Num: {0}", getNum());
nc2(5);
Console.WriteLine("Value of Num: {0}", getNum());
Console.ReadKey();
}
}
}
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)
{
// デリゲートインスタンスを作成
NumberChanger<int> nc1 = new NumberChanger<int>(AddNum);
NumberChanger<int> nc2 = new NumberChanger<int>(MultNum);
// デリゲートオブジェクトを使用してメソッドを呼び出す
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その他の拡張