Performance considerations of inheritance in C#

Does the compiler produce the same IL if I make a class with public int I; (or any other field) vs making a class that inherits from a base class that has public int I;?

Either way the resulting class behaves identically, but does the compiler behave identically?

I.e., is the compiler just copy-pasting code from base classes into the derived classes, or is it actually creating two different class objects when you inherit?

class A
{
    public int I;
}

class B : A
{
}

Now is there any performance difference between the following two DoSomething() calls?

var a = new A();
var b = new B();
DoSomething(a.I);
DoSomething(b.I); // Is this line slower than the above line due to inheritance?

Try to ignore any optimizations the compiler might do for this special case. My question is about whether inheritance in C# in general has an associated performance penalty. Most of the time I don't care, but sometimes I want to optimize a piece of high frequency code.

10
задан John Saunders 7 May 2011 в 21:55
поделиться