Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do generic interfaces in C# prevent boxing? (.NET vs Mono performance)

I have a C# interface with certain method parameters declared as object types. However, the actual type passed around can differ depending on the class implementing the interface:

public interface IMyInterface
{
    void MyMethod(object arg);
}

public class MyClass1 : IMyInterface
{
    public void MyMethod(object arg)
    {
        MyObject obj = (MyObject) arg;
        // do something with obj...
    }
}

public class MyClass2 : IMyInterface
{
    public void MyMethod(object arg)
    {
        byte[] obj = (byte[]) arg;
        // do something with obj...
    }
}

The problem with MyClass2 is that the conversion of byte[] to and from object is boxing and unboxing, which are computationally expensive operations affecting performance.

Would solving this problem with a generic interface avoid boxing/unboxing?

public interface IMyInterface<T>
{
    void MyMethod(T arg);
}

public class MyClass1 : IMyInterface<MyObject>
{
    public void MyMethod(MyObject arg)
    {
        // typecast no longer necessary
        //MyObject obj = (MyObject) arg;
        // do something with arg...
    }
}

public class MyClass2 : IMyInterface<byte[]>
{
    public void MyMethod(byte[] arg)
    {
        // typecast no longer necessary
        //byte[] obj = (byte[]) arg;
        // do something with arg...
    }
}

How is this implemented in .NET vs Mono? Will there be any performance implications on either platform?

Thank you!

like image 321
Mike Mazur Avatar asked Sep 26 '08 14:09

Mike Mazur


People also ask

Can interfaces be generic?

It's often useful to define interfaces either for generic collection classes, or for the generic classes that represent items in the collection. To avoid boxing and unboxing operations on value types, it's better to use generic interfaces, such as IComparable<T>, on generic classes.

Can I use generics in interface C#?

You can declare generic type parameters in interfaces as covariant or contravariant. Covariance allows interface methods to have more derived return types than that defined by the generic type parameters.

What does the generic constraint of type interface do?

Interface Type Constraint You can constrain the generic type by interface, thereby allowing only classes that implement that interface or classes that inherit from classes that implement the interface as the type parameter.


2 Answers

You will get the same benefits in Mono that you do in .NET.

We strongly recommend that you use Mono 1.9 or Mono 2.0 RCx in general, as generics support only matured with 1.9.

like image 110
miguel.de.icaza Avatar answered Oct 08 '22 00:10

miguel.de.icaza


The problem with MyClass2 is that the conversion of byte[] to and from object is boxing and unboxing, which are computationally expensive operations affecting performance.

There is no boxing involved with array types, even one with value type elements. An array is a reference type.

The overhead on (byte[]) arg is minimal at best.

like image 43
leppie Avatar answered Oct 08 '22 01:10

leppie