Note that there are some explanatory texts on larger screens.

plurals
  1. PODo generic interfaces in C# prevent boxing? (.NET vs Mono performance)
    primarykey
    data
    text
    <p>I have a C# interface with certain method parameters declared as <code>object</code> types. However, the actual type passed around can differ depending on the class implementing the interface:</p> <pre><code>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... } } </code></pre> <p>The problem with MyClass2 is that the conversion of <code>byte[]</code> to and from <code>object</code> is <a href="http://msdn.microsoft.com/en-us/library/yz2be5wk.aspx" rel="nofollow noreferrer">boxing and unboxing</a>, which are computationally expensive operations affecting performance.</p> <p>Would solving this problem with a <a href="http://msdn.microsoft.com/en-us/library/kwtft8ak.aspx" rel="nofollow noreferrer">generic interface</a> avoid boxing/unboxing?</p> <pre><code>public interface IMyInterface&lt;T&gt; { void MyMethod(T arg); } public class MyClass1 : IMyInterface&lt;MyObject&gt; { public void MyMethod(MyObject arg) { // typecast no longer necessary //MyObject obj = (MyObject) arg; // do something with arg... } } public class MyClass2 : IMyInterface&lt;byte[]&gt; { public void MyMethod(byte[] arg) { // typecast no longer necessary //byte[] obj = (byte[]) arg; // do something with arg... } } </code></pre> <p>How is this implemented in .NET vs Mono? Will there be any performance implications on either platform?</p> <p>Thank you!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload