Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

c#

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 a fan to say the least, not just for asthetic reasons but I don't think it makes any contribution. For example is IPSec an interface of PSec? Is IIOPConnection An interface of IOPConnection, I usually go to the definition to find out anyway.

  • So would not using this convention cause confusion?
  • Are there any c# projects or libraries of note that drop this convention?
  • Do any c# projects that mix conventions, as unfortunately Apache Wicket does?

The Java class libraries have existed without this for many years, I don't feel I have ever struggled to read code without it. Also, should the interface not be the most primitive description? I mean IList<T> as an interface for List<T> in c#, is it not better to have List<T> and LinkedList<T> or ArrayList<T> or even CopyOnWriteArrayList<T>? The classes describe the implementation? I think I get more information here, than I do from List<T> in c#.

like image 512
ng. Avatar asked Apr 28 '10 09:04

ng.


People also ask

Why do not use I as a prefix for interface names?

In general, you shouldn't prefix interfaces with I (e.g. IColor). Because the concept of an interface in TypeScript is much more broad than in C# or Java, the IFoo naming convention is not broadly useful.

Why should I code to an interface?

The technique of coding to interfaces rather than objects will improve the efficiency of the development team by: Allowing the development team to quickly establish the interactions among the necessary objects, without forcing the early definition of the supporting objects.

What is the correct way to implement an interface?

To declare a class that implements an interface, you include an implements clause in the class declaration. Your class can implement more than one interface, so the implements keyword is followed by a comma-separated list of the interfaces implemented by the class.

When should interfaces be used?

You should use an interface if you want a contract on some behavior or functionality. You should not use an interface if you need to write the same code for the interface methods. In this case, you should use an abstract class, define the method once, and reuse it as needed.


1 Answers

The difference between Java and C# is that Java allows you to easily distinguish whether you implement an interface or extend a class since it has the corresponding keywords implements and extends.

As C# only has the : to express either an implementation or extension, I recommend following the standard and put an I before an interface's name.

like image 194
Ham Vocke Avatar answered Oct 07 '22 19:10

Ham Vocke