using System.Collections;
using System;
public class Counter<T>
{
private int pivot = 0;
private readonly int arraySize = 256;
private bool startCheck = false;
T[] baskets;
public T Count
{
get
{
return baskets[pivot];
}
}
public void CountPlus(T plusValue)
{
if(!startCheck)
{
startCheck = true;
baskets[pivot] = plusValue;
}
else
{
int previousPivot = pivot;
pivot++;
if(previousPivot == arraySize - 1)
{
pivot = 0;
}
checked
{
try
{
baskets[pivot] = baskets[previousPivot] + plusValue;
}
catch(OverflowException ofe)
{
Debug.Log("=*=*=*=*=*=*=*=*= OverflowException =*=*=*=*=*=*=*=*=*=");
}
}
}
}
}
你好〜如何添加T和T
我要运行该代码,但我有一个错误信息
error CS0019: Operator '+' cannot be applied to operands of type 'T' and 'T'
我怎么解决这个问题?
'T'可以是任何类型。如果它是一个Person对象,如何将它们添加到一起?简单的答案是,似乎没有任何简单的方法可以将'T'限制为数字类型。请参阅:http://stackoverflow.com/questions/32664/c-sharp-generic-constraint-for-only-integers –
我不认为你可以。它需要对T进行约束,要求它有一个加号运算符,并且据我所知这是不可能的。 – Alxandr
你可以查看这个链接:http://stackoverflow.com/questions/1251507/is-it-possible-to-call-value-type-operators-via-reflection –