.Net 对结构类型的通用约束

发布于 2024-12-08 20:27:58 字数 245 浏览 0 评论 0原文

我正在尝试创建一个泛型类,其实现取决于 Int32Int64doublefloat 的类型>,或十进制

class Calculator<T> where T: int, double, float, decimal

这是不对的,但我在语法上遇到了麻烦

I am trying to create a generic class whose implementation depends on the type being an Int32, Int64, double, float, or decimal.

class Calculator<T> where T: int, double, float, decimal

This is not right but I'm having troubles with the syntax

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

酒废 2024-12-15 20:27:58

我正在尝试创建一个泛型类,其实现取决于 Int32、Int64、double、float 或decimal 类型。

那么它就不是通用的。泛型类型是一种对所有可能的类型参数都以相同方式工作的类型,例如泛型队列或泛型字典。

“计算器”类是此类功能最常见的场景;我们一直收到此功能请求。我们正在假设地考虑将其用于假设的 C# 未来版本,该版本尚未宣布且不存在。正确地实现该功能需要来自假设的 CLR 未来版本的大力支持,而该版本尚未公布,也不存在。随着这些事情的发展,该场景并不是一个特别高的优先级,所以如果它没有实现,请不要失望。

可以通过多种方式来呈现此类功能。例如,我们可以说您可以将静态方法放入接口中。数学运算符是静态方法,因此您可以将类型参数限制为实现静态接口 IAddable 的类型,或类似的东西。

请记住,Eric 对尚未发布的产品的未来所做的任何思考,这些产品并不存在,也可能永远不会存在,仅供您娱乐。

I am trying to create a generic class whose implementation depends on the type being an Int32, Int64, double, float, or decimal.

Then it is not generic. A generic type is one that works the same way for all possible type arguments, like a generic queue or a generic dictionary.

"Calculator" classes are the most common scenario given for a feature like that; we get this feature request all the time. We are hypothetically considering it for a hypothetical future version of C# that has not been announced and does not exist. Doing the feature right would require considerable support from a hypothetical future version of the CLR that also has not been announced and does not exist. The scenario is not a particularly high priority as these things go, so please do not be disappointed if it does not come to pass.

There are a number of ways such a feature could be surfaced. For example, we could say that you can put static methods in interfaces. Math operators are static methods, and so then you could constrain a type parameter to be a type that implements static interface IAddable, or some such thing.

Remember any musings Eric makes about the future of unannounced products that do not exist and might never exist is for your entertainment only.

单挑你×的.吻 2024-12-15 20:27:58

您不能对类型参数指定约束,使其必须是指定类型集之一,不是。请参阅MSDN 了解有效约束列表

对于那些您可以指定的特定类型:

where T : struct, IComparable, IFormattable, IConvertible,
          IComparable<T>, IEquatable<T>

这可能会很好地限制集合,尽管它也允许其他基本类型,例如 byteulong

You can't specify a constraint on a type parameter such that it has to be one of a specified set of types, no. See MSDN for a list of valid constraints.

For those particular types you could specify:

where T : struct, IComparable, IFormattable, IConvertible,
          IComparable<T>, IEquatable<T>

That's likely to restrict the set fairly well, although it would also allow other primitive types such as byte and ulong.

提笔落墨 2024-12-15 20:27:58

你不能那样做。您只能对接口、classstruct 或空构造函数 (new()) 或基类执行此操作。

请参阅:http://msdn.microsoft.com/en-us/library/d5x73970 .aspx

最接近的是 struct, IConvertable。

我见过的一种技术是使用静态构造函数并抛出断言或异常,但这不是编译时检查,只是运行时检查。

class Calculator<T>  
   where T : struct
{
   static Calculator()
   {
     Debug.Assert(typeof(T) == typeof(int), "FAIL"); //TODO: extend to other types
   }
}

You can't do that. You can only do it to an interface, class, struct, or empty constructor (new()), or a base class.

See: http://msdn.microsoft.com/en-us/library/d5x73970.aspx

The closest would be struct, IConvertable.

One technique I have seen is to use the static constructor and throw asserts or exceptions, but this is not compile time checking, only runtime.

class Calculator<T>  
   where T : struct
{
   static Calculator()
   {
     Debug.Assert(typeof(T) == typeof(int), "FAIL"); //TODO: extend to other types
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文