3
我試圖將我的genric功能拉到基類,但不同的數據類型阻止我這樣做。請參閱:創建一個通用基類
public class OutWriter
{
private readonly IEnumerable<long> series1;
private readonly IEnumerable<long> series2
private readonly IEnumerable<long> series3
private readonly IEnumerable<double> series4
private readonly double sum;
public OutWriter()
{
series1 = PopulateS1()
series2 = PopulateS2()
series3 = PopulateS3()
series4 = PopulateS4()
sum = series4.Aggregate((x,y)=>x+y);
}
}
public class PaperWriter
{
private readonly IEnumerable<BigInteger> series1;
private readonly IEnumerable<BigInteger> series2
private readonly IEnumerable<BigInteger> series3
private readonly IEnumerable<BigInteger> series4
private readonly BigInteger sum;
public PaperWriter()
{
series1 = PopulateS1()
series2 = PopulateS2()
series3 = PopulateS3()
series4 = PopulateS4()
sum = series4.Aggregate((x,y)=>x+y);
}
}
public class Base<T>()
{
????
}
我試圖拉起相似之處,但有long
,double
和參與BigInteger
。由於+操作,聚合還會拋出編譯時錯誤。你能告訴我如何適當地做到這一點嗎?
感謝, -Mike
對於這些類型來說不那麼容易,恐怕沒有可以用於這些操作符的通用約束。然而,這個問題一直出現,我相信Jon Skeet(可能是別人,別忘了)有一個很好的解決方案。它在某處,但我現在無法搜索它。 – 2012-02-23 01:23:16