Why prefix C# interface names with an “I”

前端 未结 18 2024
清歌不尽
清歌不尽 2021-02-01 03:13

What is the rationale behind this naming convention?

I don\'t see any benefit. The extra prefix just pollutes the API.

My thinking is inline with Konrad\'s respo

18条回答
  •  忘掉有多难
    2021-02-01 04:03

    Its the complete opposite, the naming convention clearly identifies an interface.

    For example if you have:

    public class Dog : IPet, IMammal
    {
        ....
    

    Just from reading it, I can safely assume that IPet and IMammal are probably interfaces.

    The .NET CLR allows for single class inheritance. So, if I have a base class..I can only inherit one class from it. Lets change the IPet interface to a base class..our example now becomes

    public class Dog : Pet, IMammal
    {
        ....
    

    I am inheriting from the Pet class and implementing the IMammal interface.

    If we did it what you are suggesting and removed the letter "I" we have this:

    public class Dog : Pet, Mammal
    {
        ....
    

    Which one is the class I am inheriting from? Which is the interface I am implementing? It gets confusing right? (FYI..you are supposed to put the base class always first, so you could argue that point...but if you are arguing to remove the letter I from prefixing interface names I doubt you follow that practice as well)

    As you can see that naming convention easily tells me a lot about my object without me having to investigate further. I can easily see what I am inheriting vs what I am implementing.

提交回复
热议问题