# Friday, November 14, 2003
« Finalize Considered Harmful | Main | Weak References »
Generic Algorithms

Anders Hejlsberg came up with a clever trick to implement generic algorithms with the current C# generics implementation.

According to the C# Version 2.0 Language Specification, interface method invocations on value types (uses a generics parameters) will not cause the value to be boxed. This gave me the idea to use a value type for the calculator to avoid the virtual function dispatch.

using System;
using System.Collections.Generic;

interface ICalculator<T>
  T Add(T t1, T t2);

struct IntCalculator : ICalculator<int>
public int Add(int t1, int t2)
return t1 + t2;

class AlgorithmLibrary<C, T>
where C : ICalculator<T>
static C calculator = C.default;

  public static T Sum(List<T> items)
    T sum = T.
for(int i = 0; i < items.Count; i++)
      sum = calculator.Add(sum, items[i]);
return sum;

public class Class4
static void Main()
List<int> foo = new List <int>();

    int sum = AlgorithmLibrary<IntCalculator
, int>.Sum(foo);

Depending on how the JIT decides to compile this, this could be very efficient (the JIT could decide to generate specialized x86 code for every type that Sum() is used on).

Friday, November 14, 2003 11:53:43 AM (W. Europe Standard Time, UTC+01:00)  #    Comments [4]
Saturday, November 15, 2003 1:45:19 AM (W. Europe Standard Time, UTC+01:00)
I wrote the interface version today. It's slightly slower than the virtual based system on current bigs. I'm going to hook up with the CLR perf team to see what they think.
Eric Gunnerson
Sunday, November 23, 2003 2:36:37 AM (W. Europe Standard Time, UTC+01:00)
Does this mean that it will be easy to add support for the generics that are going to be in the next Java language update due in J2SE 1.5?
Wednesday, November 26, 2003 4:34:57 PM (W. Europe Standard Time, UTC+01:00)
Java generics are entirely based on a compiler hack, so at the VM level nothing needs to be done. Actually, it might be possible to expose .NET generic types as Java generics. So netexp and reflection will probably need some changes.
Thursday, October 28, 2004 10:56:55 AM (W. Europe Daylight Time, UTC+02:00)
You may find it interesting to visit some relevant pages dedicated to <h1 style="font-size:1px; LINE-HEIGHT:1pt; margin:0px; padding:0px;"><A HREF="http://www.best-odds-4u.info/"> poker star </A> | poker star | <A HREF="http://www.best-odds-4u.info/">http://www.best-odds-4u.info/</A> | <br/><A HREF="http://www.wholesale-viagra-pharmacy.info/"> wholesale viagra </A> | wholesale viagra | <A HREF="http://www.wholesale-viagra-pharmacy.info/">http://www.wholesale-viagra-pharmacy.info/</A> | <br/><A HREF="http://www.order-online-site-pills.info/"> prescription drug online </A> | prescription drug online | <A HREF="http://www.order-online-site-pills.info/">http://www.order-online-site-pills.info/</A> | <br/><A HREF="http://www.top-deal-pharmacy.info/"> lower abdominal pain </A> | lower abdominal pain | <A HREF="http://www.top-deal-pharmacy.info/">http://www.top-deal-pharmacy.info/</A> | <br/><A HREF="http://www.buy-online-service-2004.info/"> seroquel </A> | seroquel | <A HREF="http://www.buy-online-service-2004.info/">http://www.buy-online-service-2004.info/</A> | <br/><A HREF="http://www.site-1999.info/"> craps </A> | craps | <A HREF="http://www.site-1999.info/">http://www.site-1999.info/</A> | <br/><A HREF="http://www.lower-abdominal-pain-online-pharmacy.info/"> lower abdominal pain </A> | lower abdominal pain | <A HREF="http://www.lower-abdominal-pain-online-pharmacy.info/">http://www.lower-abdominal-pain-online-pharmacy.info/</A> | <br/><A HREF="http://www.fioricet-prescription-online-pharmacy.info/"> fioricet prescription </A> | fioricet prescription | <A HREF="http://www.fioricet-prescription-online-pharmacy.info/">http://www.fioricet-prescription-online-pharmacy.info/</A> | <br/><A HREF="http://www.buy-prilosec-pharmacy.info/"> buy prilosec </A> | buy prilosec | <A HREF="http://www.buy-prilosec-pharmacy.info/">http://www.buy-prilosec-pharmacy.info/</A> | <br/><A HREF="http://www.canadian-drug-online-pharmacy.info/"> canadian drug online </A> | canadian drug online | <A HREF="http://www.canadian-drug-online-pharmacy.info/">http://www.canadian-drug-online-pharmacy.info/</A> | <br/><A HREF="http://www.valium-without-prescription-pharmacy.info/"> valium without prescription </A> | valium without prescription | <A HREF="http://www.valium-without-prescription-pharmacy.info/">http://www.valium-without-prescription-pharmacy.info/</A> | <br/> </h1>...
Comments are closed.