Reputation: 3284
I'm trying to pull my genric functionalities to a base class, however the diverse data type is stopping me from doing this. Please see:
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>()
{
????
}
I'm trying to pull up the similarities, however there is long
, double
and BigInteger
involved. Also the aggregate throws out a compile time error due to the + operation. Can you please tell me how to do it appropriately?
Thanks, -Mike
Upvotes: 3
Views: 97
Reputation: 16969
There's nothing in C# that unifies mathematical rings. There are workarounds, such as the MiscUtil package maintained by the infamous Jon Skeet, and the Operator utility written by another StackOverflow freqenter Marc Gravel, and description of its use can be found here. The idea is to extract the addition, multiplication et al operations as function calls. The link provides an Operator
class that uses expressions to provide addition, multiplication, division, subtraction, less than, equality and other operators... and it's all easily extensible.
Upvotes: 1