[Mono-dev] Operator overloading in C#

Rusmin Susanto rusminsusanto at yahoo.com
Tue Mar 28 21:07:26 EST 2006

Hi all.
  Could someone tell me some tricks to write a fast operator overloading in C#?
  Actually I want to minimize the number of call to "new".
  For example, I have an object that has a 2D array of double (let's say 200 by 200). I want to write operators for this class. (let's say +, -, *, /).
  I want to be able to cascade the operators (eg. A = B + C + D + E + F + G).
  The problem is:
  In every operator example that I found, a new instance of that object is always created and returned. Here is one example of a matrix object that I found:
   public static Matrix3D operator +(Matrix3D mat1, Matrix3D mat2)
  Matrix3D newMatrix = new Matrix3D();
    for (int x=0; x < DIMSIZE; x++)
   for (int y=0; y < DIMSIZE; y++)
      newMatrix[x, y] = mat1[x, y] + mat2[x, y];
    return newMatrix;
  If I need to access the operator quite often, I am afraid that a numerous calls to "new" will slow down the program (as it needs to allocate 200 x 200 array of double).
  Does anyone have tips how to solve this? (May be a trick for minimizing the number of call to "new" or a better solution may be)
  Is it worth it to convert the operator implementation to "unsafe" and use pointer to access the array directly?
  Or maybe implement the operator in C and execute it as an internal call?
  What is the best?
  Thanks in advance

New Yahoo! Messenger with Voice. Call regular phones from your PC and save big.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-devel-list/attachments/20060328/b432ed08/attachment.html 

More information about the Mono-devel-list mailing list