我想在unity中的模板方法中做比較。
public static T[] DeleteItem<T>(T[] list, T v)
{
int c = list.Length;
for (int i = 0; i < c; i++)
{
if(list[i] == v)
{
T[] list2 = new T[c-1];
for (int j = 0; j < i; j++)
{
list2[j] = list[j];
}
for (int j = i + 1; j < c; j++)
{
list2[j - 1] = list[j];
}
return list2;
}
}
return list;
}
然而上面代碼中 if(list[i] == v) 這句是報錯的。
error CS0019: Operator `==' cannot be applied to operands of type `T' and `T'
where T:System.Object 不允許。 where T:Unity.Object 可以令等號成立,大于小于號不行。并且System.string不能轉Unity.Object
這個有沒有什么辦法能讓模板函式中使用 > == < ?
uj5u.com熱心網友回復:
用 object.ReferenceEquals或者Equals或者CompareTo(實作了ICompareable介面)等代替。或者用運算式樹動態呼叫==
uj5u.com熱心網友回復:
型別不確定,所以她無法進行比較的。你可以用 屬性里的 GetValue()進行比較。uj5u.com熱心網友回復:
因為沒有運算子多載。轉載請註明出處,本文鏈接:https://www.uj5u.com/net/124777.html
標籤:C#
上一篇:.net core 使用 Microsoft.Office.Interop.Excel 轉換成PDF COM 組件報錯。
