# Friday, 14 November 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, 14 November 2003 11:53:43 (W. Europe Standard Time, UTC+01:00)  #    Comments [4]
Saturday, 15 November 2003 01:45:19 (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, 23 November 2003 02:36:37 (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, 26 November 2003 16:34:57 (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, 28 October 2004 10:56:55 (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>...
Home page

I apologize for the lameness of this, but the comment spam was driving me nuts. In order to be able to post a comment, you need to answer a simple question. Hopefully this question is easy enough not to annoy serious commenters, but hard enough to keep the spammers away.

Anti-Spam Question: What method on java.lang.System returns an object's original hashcode (i.e. the one that would be returned by java.lang.Object.hashCode() if it wasn't overridden)? (case is significant)

Comment (HTML not allowed)  

Live Comment Preview