Why prefix C# interface names with an “I”

前端 未结 18 1573
清歌不尽
清歌不尽 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:02

    I always thought it was fun to use verbs for behavioral interfaces. This is a departure from the class naming convention of using nouns, but it allows the class to "speak" to its behavior.

    class Dog: IBark
    

    This does not work well for structural interfaces like WCF interfaces, but we don't need to have fun all the time.

    to answer your question, think of the I as "implements" So...

    class DogDataService : Dog, IDataService
    

    this service class inherits from Dog and implements IDataService

    I'm still not really answering your question, but the I is useful because you get naming collisions between namespace, class and interface.

    namespace DataService
    interface DataService
    class DataService: DataService
    

    so we end up with

    namespace DataServices
    interface IDataService
    class DataService : IDataService
    

    I think in reality, it's a sanity convention.

提交回复
热议问题