Ofek Ron
Ofek Ron

Reputation: 8578

Converting Java Generics to C# Generics

That is my Java code for a Generic bubbleSorter:

public class BubbleSorter<E extends Comparable<E>> {
    E[] a;
    void swap(int i, int j) {
        E temp;
        temp=a[i];
        a[i]=a[j];
        a[j]=temp;
    }
    void bubbleSort(E[] a) {
        this.a=a;
        for (int i=0 ;i<a.length;i++) {
            for (int j=0;j<a.length;j++) {
                if ( a[i].compareTo(a[j]) > 0) swap(i,j);
            }
        }
    }

}

public interface Comparable<E> {
    public int compareTo(E e);
}

and here is an example of its use:

public class Test { 
    public static void main (String arg[]) {
        Rational[] a = new Rational[3];
        a[0]=Rational.rationalFactory(9,2);
        a[1]=Rational.rationalFactory(1,3);
        a[2]=Rational.rationalFactory(10,11);
        Complex[] b = new Complex[3];
        b[0]=new Complex(7,5);
        b[1]=new Complex(3,4);
        b[2]=new Complex(8,9);
        BubbleSorter<Rational> br=new BubbleSorter<Rational>();
        BubbleSorter<Complex> bi=new BubbleSorter<Complex>();
        br.bubbleSort(a);
        bi.bubbleSort(b);
        for (int i=0 ; i < 3 ; i++ ) {
            System.out.print(a[i] + " ");
        }
        System.out.println();
        for (int i=0 ; i < 3 ; i++ ) {
            System.out.print(b[i] + " ");
        }
    }
}

public class Rational implements Comparable<Rational> {
    int mone,mehane;
    private Rational(int n,int m) {
        mone=n;
        mehane=m;
    }
    static public Rational rationalFactory(int n,int m) {
        if (n==0) return null;
        return new Rational(n,m);
    } 
    public String toString() {
        return mone + "/" + mehane;
    }
    public int compareTo(Rational r) {
        return (r.mehane*mone - r.mone*mehane);
    }
}
public class Complex implements Comparable<Complex> {
        int real,img;
        public Complex(int r,int i) {
            real=r;
            img=i;
        }
        public String toString() {
            return real + "+" + img + "i";
        }
        public int compareTo(Complex r) {
            double x=(getLength() - r.getLength());
            if (x>0) return 1;
            if (x==0) return 0;
            return -1;
        }
        public double getLength() {
            return Math.sqrt(real*real+img*img);
        }
}

When I tried to convert my Java code to C#, I got stuck trying to force the generic type to extend Comparable since < E : Comparable > doesnt work. How can i overcome this?

that is what i tried :

abstract class Comparable<E> {

    static bool operator ==( Comparable<E> e1, Comparable<E> e2 );
    static bool operator !=( Comparable<E> e1, Comparable<E> e2 ) {
        return !( e1 == e2 );
    }
    static bool operator >( Comparable<E> e1, Comparable<E> e2 );
    static bool operator >=( Comparable<E> e1, Comparable<E> e2 ) {
        if ( e1 > e2 ) return true;
        if ( e1 == e2 ) return true;
        return false;
    }
    static bool operator <=( Comparable<E> e1, Comparable<E> e2 ) {
        return !( e1 > e2 );
    }
    static bool operator <( Comparable<E> e1, Comparable<E> e2 ) {
        return !( e1 >= e2 );
    }
}

public class BubbleSorter<E : Comparable<E>> {
        E[] a;
        void swap(int i, int j) {
            E temp;
            temp=a[i];
            a[i]=a[j];
            a[j]=temp;
        }
        void bubbleSort(E[] a) {
            this.a=a;
            for (int i=0 ;i<a.length;i++) {
                for (int j=0;j<a.length;j++) {
                    if ( a[i]>a[j] ) swap(i,j);
                }
            }
        }

}

Upvotes: 2

Views: 388

Answers (4)

Monroe Thomas
Monroe Thomas

Reputation: 5042

In C#, one usual idiomatic way to implement comparability for a type is to have it derive from IComparable<T>. If you can't change the type to implement IComparable, then you can implement a helper class that implements IComparer<E>.

public class BubbleSorter<E> 
{
    static void Swap(E[] a, int i, int j) 
    { 
        E temp; 
        temp=a[i]; 
        a[i]=a[j]; 
        a[j]=temp; 
    } 

    public void BubbleSort(E[] a, IComparer<E> comparer) 
    { 
        for (int i=0 ;i<a.length;i++) { 
            for (int j=0;j<a.length;j++) { 
                if ( comparer.Compare(a[i],a[j]) > 0 ) swap(a,i,j); 
            } 
        } 
    } 
 } 

Upvotes: 1

O. R. Mapper
O. R. Mapper

Reputation: 20760

The keyword to use for generic constraints in C# is where.

Therefore, first declare the signature of your generic type:

public class BubbleSorter<E>

Then define the generic constraints:

    where E : IComparable<E>

A word on coding conventions: In C#, it is customary to call a single generic argument T (like type) rather than E (like element). That's the pattern used in all the framework collection classes, so you might want to adapt your type name:

public class BubbleSorter<T>
    where T : IComparable<T>
{
    // ...
}

Behind the colon (:), you can specify a comma-separated list of interfaces and possibly a class name. The compiler knows which is which, so you don't have to explicitly specify whether you want to implement (an interface) or inherit (from a class).

Upvotes: 2

miniBill
miniBill

Reputation: 1732

This is C# syntax:

public class BubbleSorter<E> where E : Comparable<E>

Upvotes: 1

dlev
dlev

Reputation: 48596

You should use the built-in IComparable<T> interface, and then declare your class as

public class BubbleSorter<T> where T : IComparable<T> { ... }

The where keyword defines a "constraint" on the generic parameter T. The compiler will enforce this constraint by ensuring that for any instantiation of the generic class, the type argument implements the IComparable<T> interface.

Upvotes: 7

Related Questions