Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3635686
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T00:56:07+00:00 2026-05-19T00:56:07+00:00

I’ve been led to believe that casting can, in certain circumstances, become a measurable

  • 0

I’ve been led to believe that casting can, in certain circumstances, become a measurable hindrance on performance. This may be moreso the case when we start dealing with incoherent webs of nasty exception throwing\catching.

Given that I wish to create more correct heuristics when it comes to programming, I’ve been prompted to ask this question to the .NET gurus out there: Is interface casting faster than class casting?

To give a code example, let’s say this exists:

public interface IEntity { IParent DaddyMommy { get; } }
public interface IParent : IEntity { }
public class Parent : Entity, IParent { }
public class Entity : IEntity
{
    public IParent DaddyMommy { get; protected set; }
    public IParent AdamEve_Interfaces
    {
        get
        {
            IEntity e = this;
            while (e.DaddyMommy != null)
                e = e.DaddyMommy as IEntity;
            return e as IParent;
        }   
    }
    public Parent AdamEve_Classes
    {
        get
        {
            Entity e = this;
            while (e.DaddyMommy != null)
                e = e.DaddyMommy as Entity;
            return e as Parent;
        }
    }
}

So, is AdamEve_Interfaces faster than AdamEve_Classes? If so, by how much? And, if you know the answer, why?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T00:56:07+00:00Added an answer on May 19, 2026 at 12:56 am

    Take a look at here:

    http://thatstoday.com/robbanp/blog/6/25/csharp-performance–cast-vs-interface

    And, yes, you seem to be right.

    Edit 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).

    I do have some spare time nowadays, so I’ve written a simple performance measuring 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 < 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 < 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 < 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 < Cycles; i++)
            {
                obj.SameProperty = i;
                var j = obj.SameProperty;
                obj.SameProperty = j;
            }
    
            return watch.ElapsedMilliseconds;
        }
    }
    

    And the result is:

    simpleClassTime = 274
    interfacedClassTime = 339
    simpleStructTime = 247
    interfacedStructTime = 302
    

    I really used to think that an interface would be faster for class types, and slower for struct (since boxing/unboxing is involved in the latter), but that is not the case: a concrete class/struct reference is always faster, it seems.

    Also, to whom it may concern: I believe that performance is not a good criteria for deciding if an interface should or should not be used. The difference is, like others said here, negligible.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.