C#中的泛型:编译时错误,'+'无法应用

问题描述:





我正在尝试在泛型类中使用泛型方法。但是我收到错误



运算符'+'不能应用于'T'和'T'类型的操作数



我的代码是 -



Hi,

I am trying to use a generic method within a generic class.But am getting an error as

Operator '+' cannot be applied to operands of type 'T' and 'T'

My code is as -

public class Gen<T>
{
  public T Plus<T>(T par1, T par2)
    {
        return par1 + par2;
    }
}





任何想法,伙计们?

问候

Rajeev



Any ideas, folks ??
Regards
Rajeev

这是一个大问题。这是非常想要的行为,但.NET完全不可能。

为什么这个东西不能编译。因为没有为通用参数类型定义操作'+',' - '等。即使您定义了此操作,也需要使用关键字where将约束添加到泛型类型声明中。它可以工作,但是!约束不能是原始类型!



这是一般困扰.NET的两个或三个最大的架构问题之一。为什么会这样?因为,尽管常见的类型系统(装箱/拆箱).NET未能引入继承是原始类型(这不是神秘或不可能的东西;例如,在Ada中也是如此)。在.NET中你甚至不能写C ++ typedef (这也不是一个类型定义)。



那里是一个已知的解决方法。数值计算的泛型类不能使用基本类型,而是使用其包装类,并且类或接口可以是泛型类中的约束。但是让我们看看它的成本是多少!



要开发这样的约束,你需要开发一个包括所有算术运算和所有系统的类。数学操作。那么混合型操作数的操作怎么样?那是为了什么?你会用做一次这么费时的事情,用加上做一次,还有什么呢?这是一项长期无聊的工作,不会真正得到回报(但会有所降低)。放弃赞成,比如说,只使用 double 类型会更加实用。



不幸的是......



-SA
This is a big problem. This is very wanted behavior but completely impossible with .NET.
Why this thing will not compile. Because the operations '+', '-', etc. are not defined for the generic parameter type. Even if you define this operation, you need to add the constraint to the generic type declaration using the key word "where". It can work, but! The constraint could not be a primitive type!

This is one of two or three biggest architectural problems which haunt .NET in general. Why it is so? Because, despite of common type system (boxing/unboxing) .NET failed to introduce inheritance is primitive type (which is not something mystical or impossible; so is done, for example, in Ada). In .NET you cannot even write C++ typedef (which is not a type definition as well).

There is a known work-around. The generic class for numeric calculation can operate not with primitive types, but with its wrapper classes, and the class or interface can be a constraint in the generic classes. But let's see how much it costs!

To develop such constraints, you will need to develop a class including all arithmetic operations plus all System.Math operations. And how about operation with mixed-type operands? And what for? You will do such time-consuming thing once with single, and once with double, and what else? It's a long boring work which won't really pay off (but will somewhat decrease performance). Giving up in favor of, say, using double type only would be much more practical.

Unfortunately…

—SA


您好如果要一起添加类型,那么您需要在该类中定义如何添加它们。请参阅下面的示例。



Hi if you want to add a type of class together then you need to define in that classes how to add them. See the example below.

public class t{
     public int Value;
     public static t operator +(t obj1,t obj2)
     {
          obj1.Value +=  obj2.Value;
          return obj1;
     }
 }
 public class Gen<T>
 {
     public t Plus/*removed <T>*/(t par1, t par2) //SA!!! 2011-18-03: a fix to make code compile
     {
         return par1 + par2;
     }
 }







现在你可以像..这样使用.. br $> b $ b






Now you can use this like..


Gen<t> test = new Gen<t>();
t test1 = new t();
test1.Value = 40;
t test2 = new t();
test2.Value = 50;
t test3 = test.Plus<t>(test1, test2);
MessageBox.Show(test3.Value.ToString());





根据您在问题中显示的示例。



As per the example you have shown in the question.


无保证 ,但尝试重载班级中的+运算符



No guarantees, but try overloading the + operator in your class

public static Gen<T> operator +(Gen<T> a, Gen<T> b)
{
    return (Gen<T>)(a.Value + b.Value);
}





编辑(2016年5月31日)---------------- ---------------------------------------



对于那些错过它的人,我在无保证的前提下给我答案。为了帮助感知障碍者,我用粗体和斜体标出符合条件的文本,这样更容易看到。



用一个人投票给我的答案是bull(这是我不再参加Q / A的原因之一。)



EDIT (31 May 2016) -------------------------------------------------------

For those that missed it, I prefaced my answer with "no guarantees". To assist the perception-impaired, I bolded and italicized the qualifying text so it's easier to see.

Voting my answer with a one is bullsh|t (and this is one of the reasons I no longer participate in Q/A).