How to use, where for operators in C # generics class?

Hi, I would like to have a class:

class Matrix <T> where T : // I don't know what to write here { T[][] elements; } 

I would like T to be incremental and multiple using the + and * operators

+6
source share
4 answers

I highly recommend you take a look at the MiscUtil library from Messrs Skeet and Gravell:

http://www.yoda.arachsys.com/csharp/miscutil/

Contained inside - this is an incredible implementation of "universal mathematical operators", which should go well with your attempts to create a common mathematical class. It actually reminds me of (a bit) python and how it directly refers to operational functions.

(aside @ jon-skeet: does any chance of this make its way to Nuget?)

Some examples use their tests:

 double sumOperator = 0; for (int i = 0; i < COUNT; i++) { sumOperator = Operator.Add(sumOperator, rand.NextDouble()); } int sumOperator = 0; for (int i = 0; i < COUNT; i++) { sumOperator = Operator.Add(sumOperator, rand.Next(MAXVALUE)); } 
+2
source

Since I do not know any built-in solutions, check this:

 public abstract class BaseClass { public static BaseClass operator +(BaseClass p1, BaseClass p2) { return p1 + p2; } public static BaseClass operator *(BaseClass p1, BaseClass p2) { return p1 * p2; } } public class ChildClass : BaseClass { } public class Matrix <T> where T : BaseClass { T[][] elements; } 

Hope this helps.

However , other types, such as int , are not supported.

+1
source

Perhaps this method will be useful for someone. You can improve this by adding multiply functions to the fabric-library class.

UPDATE It may be that Operators from the post of JerKimball got into a consultant

 class Matrix <T> { T[][] elements; public Matrix(Func<T, T, T> add, Func<T,T,T> multiplicate) { this.Add = add; this.Mult = multiplicate; } public Matrix<T> operator +(Matrix<T> p1, Matrix<T> p2) { // correct this var t = Add(p1.elements[0][0], p2.elements[0][0]); return this; } public Matrix<T> operator *(Matrix<T> p1, Matrix<T> p2) { // correct this var t = Mult(p1.elements[0][0], p2.elements[0][0]); return this; } private Func<T, T, T> Add { get; set; } private Func<T, T, T> Mult { get; set; } } static void Main(string[] args) { var m1 = new Matrix<int>((x,y) => x + y, (x,y) => x * y); var m2 = new Matrix<int>((x, y) => x + y, (x, y) => x * y); } 

UPDATE 2

just grant access to the following properties:

  public T[][] Elements { get; set; } 

and you can do this:

 m1.Elements[0][0] = 10; var m3 = new Matrix<int?>((x, y) => x + y, (x, y) => x * y); m3.Elements[0][0] = null; 
+1
source

This is not possible since it is assumed that an unbounded generic type is of type Object , which does not define arithmetic operators. Therefore, it seems you need to specify an interface.

But primitive types (Int32, Double, etc.) effectively define arithmetic operations as static methods (in fact, the compiler treats them specifically and generates embedded code), and the C # interface cannot define static methods (although the CLR design itself actually allows this). Therefore, no interface is written in C # that satisfies the requirements of supporting arithmetic operations; even if it can be written, primitive types do not inherit it, so it still won’t work.

The end result is that in C # there is no way to do what you want.

If you try to do this, you will get an error similar to

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

If you do an online search for this post, you will find various discussions about possible workarounds.

+1
source

All Articles