是否有一个C#泛型约束的"实数"类型? [英] Is there a C# generic constraint for "real number" types?

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

问题描述

可能重复:结果
   C#泛型约束只整数

映入眼帘!

我试图建立在C#中的直角坐标系,但我不希望自己限制于任何一个数值类型为我的坐标值。有时,他们可能是整数,而其他时候,他们可能是有理数,根据上下文。

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);

如果我想这个级别的自由,我会选出一个typeof运算(T)的噩梦,当涉及到我的计算类中,淘汰的bool,字符串,对象,等等?更糟的是,我是选举使一个类为每种类型的号码我想工作,具有相同的内部数学公式?

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?

任何帮助将是AP preciated。谢谢!

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;
    }
}

同样的,定义一个 FloatCalculator ,你需要的任何操作。这不是特别快,但比C#4.0 <更快href=\"http://blogs.msdn.com/lucabol/archive/2009/02/05/simulating-inumeric-with-dynamic-in-c-4-0.aspx\"><$c$c>dynamic构建。

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);

一个副作用是,你将只能够实例计算器如果你传递给它的类型有一个匹配的计算器&LT; T&GT; 执行,这样你就不必做运行时类型检查。

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.

这基本上是Hejlsberg为在这次采访那里的问题进行了讨论参照。我个人还是希望看到一些基本类型:)

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#泛型约束的&QUOT;实数&QUOT;类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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