To use the 'I' prefix for interfaces or not to

前端 未结 20 1430
长情又很酷
长情又很酷 2021-02-01 02:54

That is the question? So how big a sin is it not to use this convention when developing a c# project? This convention is widely used in the .NET class library. However, I am not

相关标签:
20条回答
  • 2021-02-01 03:38

    Look at the BCL. In the Base Class Libraries you have IList<>, IQueryable, IDisposable. If you don't prepend it with a 'I', how would people know it's an interface other than going to the definition?

    Anyways, just my 2 cents

    0 讨论(0)
  • 2021-02-01 03:39

    Ask yourself: If my IDE could give me some hint in the text (e.g different colour, underline, italic...) that the type was an interface would I still bother?

    Sounds like you are naming the types like that just so you can tell from the name something about parts of the definition other than the name.

    0 讨论(0)
  • 2021-02-01 03:39

    I don't see any good reason to do this. 'Extends' vs 'implements' already tells you whether you are dealing with a class or an interface in the cases where it actually matters. In all other cases the whole idea is that you don't care.

    0 讨论(0)
  • 2021-02-01 03:42

    With respect, in your post you are only considering your needs (I, I, I), and not the needs of the readers of your code. If you are a one-man shop, then fair enough, but if your code if ever read by others, then consider that they will be expecting interfaces to have an I prefix--that is just the way it is in .Net, and too many people are used to it to change now.

    Also, it would help if you used more readable names for classes. What is PSec? How can I tell whether IPSec is an interface, when I can't even tell what PSec is? If instead PSec was renamed to e.g., PersonalSecurity, then IPersonalSecurity is much more likely to be an interface.

    0 讨论(0)
  • 2021-02-01 03:43

    I would say that not following this convention would get you down to .NET hell. It's a convention that's almost as important to me as using self in instance methods in Python.

    0 讨论(0)
  • 2021-02-01 03:43
    1. It's not a sin per se, it's best practice. It makes things a lot more readable all in all. Also, think about it. IMyClass is the interface to MyClass. It just makes sense, and stops unnecessary confusion. Also remember the : syntax vs. implements/extends. Lastly, you can bypass all of this by simply checking the tooltips/go to in VS, but for pure readability, the standard is important in my opinion.

    2. Not that I'm aware of, but I'm sure they exist.

    3. Haven't seen any, but I'm sure they exist.

    0 讨论(0)
提交回复
热议问题