Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the C# compiler explicitly declare all interfaces a type implements?

Tags:

c#

.net

clr

cil

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"; }
    }
}
like image 441
MichaelGG Avatar asked Apr 17 '09 06:04

MichaelGG


People also ask

Why does the letter c exist?

The letter c was applied by French orthographists in the 12th century to represent the sound ts in English, and this sound developed into the simpler sibilant s.

Does the letter c exist?

C, or c, is the third letter in the English and ISO basic Latin alphabets. Its name in English is cee (pronounced /ˈsiː/), plural cees. This article contains phonetic transcriptions in the International Phonetic Alphabet (IPA).

Why does c have two sounds?

History. This alternation is caused by a historical palatalization of /k/ which took place in Late Latin, and led to a change in the pronunciation of the sound [k] before the front vowels [e] and [i].

Why does the letter Q exist?

q, seventeenth letter of the modern alphabet. It corresponds to Semitic koph, which may derive from an earlier sign representing the eye of a needle, and to Greek koppa. The form of the majuscule has been practically identical throughout its known history.


1 Answers

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.
like image 130
Sander Avatar answered Sep 21 '22 23:09

Sander