IArithmetic <T> interface in C #

I studied the link to the C # source code. And I came across an interesting mention of the IArithmetic<T> interface. For example, Int32 , Double contain commented implementations of the IArithmetic interface. I am interested in these details. As I understand it, this is an attempt to add "support" to arithmetic operations. But why are they commenting? Is this a bad way to add supporting generic "operators"?

+6
source share
1 answer

This was probably removed due to performance considerations and not very convenient use.

Primitive types that support arithmetic operations through an interface are really not a very attractive scenario; performance will be terrible compared to just using the value type itself due to the necessary boxing and unboxing.

What is the possible use? Well, the first one to spring into the head would be the following scenario:

 public Matrix<T> where T: IArithmetic<T> 

or some such. Although this may be interesting, for performance reasons, it will probably need to be solved in a different way, rather than through interfaces; read this for very educated thoughts on this subject.

Also, if you really need something similar to Arithmetic<T> , you can always create your own with an added level of indirection.

+3
source

Source: https://habr.com/ru/post/1014378/


All Articles