How can I subtract two generic objects (T - T) in C# (Example: DateTime - DateTime)?

后端 未结 6 1757
再見小時候
再見小時候 2020-12-30 04:42

I wrote a Generic Class:

public class Interval where T : IComparable // for checking that Start < End 
{
    public T Start { ge         


        
6条回答
  •  一向
    一向 (楼主)
    2020-12-30 05:21

    While this may seem like a major restriction, you need to remember that generics are generic. Of course, the System.Int32 type can work just fine with the binary operators of C#. However, for the sake of argument, if were a custom class or structure type, the compiler cannot assume it has overloaded the +, -, *, and / operators.

提交回复
热议问题