tags:

views:

154

answers:

1

The C# compiler seems to explicitly note all interfaces it, and its base classes implement. The CLI specs say that this is not necesary. I've seen some other compilers not emit this explicitly, and it seems to work fine. Is there any difference or reason the C# does this?

The MSIL that the C# at the bottom generates for B is:

.class private auto ansi beforefieldinit B
       extends A
       implements IAdvanced,
                  ISimple

It shouldn't need to specify ISimple, because A implements it as does IAdvanced. C# code:

interface ISimple {
    int Basic { get;  }
    int Zero { get;  }
}
interface IAdvanced : ISimple {
    string Major { get; }
}
class A : ISimple {
    int ISimple.Basic {
        get { return 1; }
    }
    int ISimple.Zero {
        get{ return 0;}
    }
}
class B : A, IAdvanced {
    string IAdvanced.Major {
        get { return "B"; }
    }
}
+1  A: 

I don't think we can know any definitive answer here, unless we have the compiler developers drop in. However, we can guess about the reasons. It could be:

  1. for optimization - perhaps it saves some work for the JIT compiler.
  2. for readability - makes it easier for human eyes to grasp what a type implements when looking at the MSIL output.
  3. because that's how the summer intern implemented it and since it works fine, nobody is going to change it just in case it breaks something.
Sander
IIRC, everything is clearly defined in the spec. So no guessing here.
leppie
I'd go for door number three! ;-)
peSHIr
Yea, Partition II of the CLI specs make it pretty clear that it's not required. I didn't see if it said that adding it actually does anything though.
MichaelGG