Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IArithmetic<T> interface in c# [closed]

I explored c# source code reference. And I came across with interesting mention of IArithmetic<T> interface. For example, Int32, Double contain commented implementations of IArithmetic interface. I am interested by these details. As I understood, it is attempt to add "supporting" of arithmetic operations. But why are they commented? Is it bad way to add supporting generic "operators"?

like image 985
LmTinyToon Avatar asked Jan 24 '17 16:01

LmTinyToon


1 Answers

It was probably scrapped due to performance reasons and not very much usability.

Primitive types supporting arithmetic operations through an interface is really not a very attractive scenario; performance would be horrible compared to simply using the value type itself due to the necessary boxing and unboxing.

What possible uses? Well, the first one to spring to mind would be the following scenario:

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

or some such. Although this could be interesting, due to performance reasons, it would probably need to be solved some other way, not through interfaces; read this for very educated musing on the subject.

On top of all that, if you really need something similar to Arithmetic<T> you can always build your own with an added level of indirection.

like image 66
InBetween Avatar answered Sep 29 '22 09:09

InBetween