Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Take a look at here:</p> <p><a href="http://thatstoday.com/robbanp/blog/6/25/csharp-performance--cast-vs-interface" rel="noreferrer">http://thatstoday.com/robbanp/blog/6/25/csharp-performance--cast-vs-interface</a></p> <p>And, yes, you seem to be right.</p> <p><strong>Edit</strong> Well, it seems that I was wrong. And like my "patrício" Martinho Fernandes commented bellow, the above link is completely bogus (but I'll keep it here, for the sake of honest editing).</p> <p>I do have some spare time nowadays, so I've written a simple performance measuring code:</p> <pre><code>public partial class Form1 : Form { private const int Cycles = 10000000; public interface IMyInterface { int SameProperty { get; set; } } public class InterfacedClass : IMyInterface { public int SameProperty { get; set; } } public class SimpleClass { public int SameProperty { get; set; } } public struct InterfacedStruct : IMyInterface { public int SameProperty { get; set; } } public struct SimpleStruct { public int SameProperty { get; set; } } public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { var simpleClassTime = MeasureSimpleClass(); var interfacedClassTime = MeasureInterfacedClass(); var simpleStructTime = MeasureSimpleStruct(); var interfacedStructTime = MeasureInterfacedStruct(); var message = string.Format( "simpleClassTime = {0}\r\ninterfacedClassTime = {1}\r\nsimpleStructTime = {2}\r\ninterfacedStructTime = {3}", simpleClassTime, interfacedClassTime, simpleStructTime, interfacedStructTime ); textBox.Text = message; } private static long MeasureSimpleClass() { var watch = Stopwatch.StartNew(); var obj = new SimpleClass(); for (var i = 0; i &lt; Cycles; i++) { obj.SameProperty = i; var j = obj.SameProperty; obj.SameProperty = j; } return watch.ElapsedMilliseconds; } private static long MeasureInterfacedClass() { var watch = Stopwatch.StartNew(); IMyInterface obj = new InterfacedClass(); for (var i = 0; i &lt; Cycles; i++) { obj.SameProperty = i; var j = obj.SameProperty; obj.SameProperty = j; } return watch.ElapsedMilliseconds; } private static long MeasureSimpleStruct() { var watch = Stopwatch.StartNew(); var obj = new SimpleStruct(); for (var i = 0; i &lt; Cycles; i++) { obj.SameProperty = i; var j = obj.SameProperty; obj.SameProperty = j; } return watch.ElapsedMilliseconds; } private static long MeasureInterfacedStruct() { var watch = Stopwatch.StartNew(); IMyInterface obj = new InterfacedStruct(); for (var i = 0; i &lt; Cycles; i++) { obj.SameProperty = i; var j = obj.SameProperty; obj.SameProperty = j; } return watch.ElapsedMilliseconds; } } </code></pre> <p>And the result is:</p> <pre><code>simpleClassTime = 274 interfacedClassTime = 339 simpleStructTime = 247 interfacedStructTime = 302 </code></pre> <p>I really used to think that an interface would be faster for <code>class</code> types, and slower for <code>struct</code> (since boxing/unboxing is involved in the latter), but that is not the case: a concrete class/struct reference is always faster, it seems.</p> <p>Also, to whom it may concern: I believe that performance is <strong>not</strong> a good criteria for deciding if an interface should or should not be used. The difference is, like others said here, negligible.</p>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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