是否存在用于“实数”的C#泛型约束类型? [英] Is there a C# generic constraint for "real number" types?

查看:242
本文介绍了是否存在用于“实数”的C#泛型约束类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

只对整数的C#泛型约束


Greets!



我试图在C#中设置一个笛卡尔坐标系,但我不想限制自己的坐标值为任何一个数字类型。有时它们可​​以是整数,其他时候它们可以是有理数,取决于上下文。



这是尖叫通用类给我,如何将类型压缩为积分和浮点。我似乎找不到一个涵盖任何实数概念的类...

  public class Point< T&其中T:[SomeClassThatIncludesBothIntsandFloats?] {
T myX,myY;

public Point(T x,T y){
myX = x;
myY = y;
}
}

Point< int> pInt = new Point< int>(5,-10);
Point< float> pFloat = new Point< float>(3.14159,-0.2357);

如果我想要这样的自由,我选择一个typeof它涉及到我的类中的计算,除草bools,字符串,对象等?或者更糟糕的是,我选择为每种类型的数字做一个类,每个都有相同的内部数学公式?



任何帮助将不胜感激。谢谢!

解决方案

您不能定义这样的约束,但可以在运行时检查类型。这不会帮助你做计算。



如果你想做计算,这样的选项是一个选项:

 类计算< T,S>其中S:计算器< T>,new()
{
计算器< T> _calculator = new S();

public T Square(T a)
{
return _calculator.Multiply(a,a);
}

}

抽象类计算器< T>
{
public abstract T Multiply(T a,T b);
}

class IntCalculator:Calculator< int>
{
public override int Multiply(int a,int b)
{
return a * b;
}
}

同样,定义一个 FloatCalculator 和您需要的任何操作。它不是特别快,虽然比C#4.0 dynamic

 code> var calc = new Computulations< int,IntCalculator>(); 
var result = calc.Square(10);

副作用是你只能实例化 / code>如果你传递给它的类型具有匹配的 Calculator< T> 实现,所以你不必进行运行时类型检查。 >

这基本上是Hejlsberg在这个采访,讨论问题。个人我仍然想看到某种基本类型:)


Possible Duplicate:
C# generic constraint for only integers

Greets!

I'm attempting to set up a Cartesian coordinate system in C#, but I don't want to restrict myself to any one numerical type for my coordinate values. Sometimes they could be integers, and other times they could be rational numbers, depending on context.

This screams "generic class" to me, but I'm stumped as to how to constrict the type to both integrals and floating points. I can't seem to find a class that covers any concept of real numbers...

public class Point<T> where T : [SomeClassThatIncludesBothIntsandFloats?]  {
    T myX, myY;

    public Point(T x, T y) {
        myX = x;
        myY = y;
    }
}

Point<int> pInt = new Point<int>(5, -10);
Point<float> pFloat = new Point<float>(3.14159, -0.2357);

If I want this level of freedom, am I electing for a "typeof(T)" nightmare when it comes to calculations inside my classes, weeding out bools, strings, objects, etc? Or worse, am I electing to make a class for each type of number I want to work with, each with the same internal math formulae?

Any help would be appreciated. Thanks!

解决方案

You can't define such a constraint, but you could check the type at runtime. That won't help you for doing calculations though.

If you want to do calculations, something like this would be an option:

class Calculations<T, S> where S: Calculator<T>, new()
{
    Calculator<T> _calculator = new S();

    public T Square(T a)
    {
        return _calculator.Multiply(a, a);
    }

}

abstract class Calculator<T>
{
    public abstract T Multiply(T a, T b);
}

class IntCalculator : Calculator<int>
{
    public override int Multiply(int a, int b)
    {
        return a * b;
    }
}

Likewise, define a FloatCalculator and any operations you need. It's not particularly fast, though faster than the C# 4.0 dynamic construct.

var calc = new Calculations<int, IntCalculator>();
var result = calc.Square(10);

A side-effect is that you will only be able to instantiate Calculator if the type you pass to it has a matching Calculator<T> implementation, so you don't have to do runtime type checking.

This is basically what Hejlsberg was referring to in this interview where the issue is discussed. Personally I would still like to see some kind of base type :)

这篇关于是否存在用于“实数”的C#泛型约束类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆