重複の可能性:
.NET ジェネリックでオーバーロードされた演算子制約の解決策 ジェネリック
で算術を実装していますか?
Generics クラスを作成しましたが、タイトルに記載されている問題が発生しています。
class Program
{
static void Main(string[] args)
{
int a = 1;
int b = 2;
int c = 3;
dynamic obj = new Gen<int>();
obj.TestLine1(ref a, ref b);
obj = new Gen<string>();
obj.TestLine2(ref a, ref b, ref c);
System.Console.WriteLine(a + " " + b);
Console.ReadLine();
}
}
public class Gen<T>
{
public void TestLine1(ref T a, ref T b)
{
T temp;
temp = a;
a = b;
b = temp;
}
public void TestLine2(ref T a, ref T b, ref T c)
{
T temp;
temp = a;
a = a + b;
b = a + c;
c = a + b;
}
}
メソッドTestLine2(ref T a、ref T b、ref T c)の内部で、以下の問題が発生しています:
Operator '+' cannot be applied to operands of type 'T' and 'T'