创建通用基础
我试图将我的通用功能拉到基类中,但是不同的数据类型阻止了我这样做。请参阅:
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
。由于 + 操作,聚合还会抛出编译时错误。你能告诉我如何正确地做到这一点吗?
谢谢, -麦克风
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
C# 中没有任何东西可以统一数学环。有一些解决方法,例如臭名昭著的 Jon Skeet 维护的 MiscUtil 包,以及另一个 StackOverflow 频率输入者 Marc Gravel 编写的 Operator 实用程序,其使用说明可以在 此处。这个想法是将加法、乘法等操作提取为函数调用。该链接提供了一个
Operator
类,它使用表达式来提供加法、乘法、除法、减法、小于、等于和其他运算符......并且它都很容易扩展。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.