Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# naming convention for extension methods for interface

I typically name my C# interfaces as IThing. I'm creating an extension method class for IThing, but I don't know what to name it. On one hand, calling it ThingExtensions seems to imply it is an extension class to some Thing class instead of to the IThing interface. It also makes the extension class be sorted away from the interface it extends, when viewing files alphabetically. On the other hand, naming it IThingExtensions makes it look like it is an interface itself, instead of an extension class for an interface. What would you suggest?

Edit: there is not a Thing class that implements IThing, in response to some of the comments.

like image 455
Sarah Vessels Avatar asked Apr 23 '10 17:04

Sarah Vessels


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


3 Answers

I definitely prefer the name ThingExtensions over IThingExtensions. The reason being that to most programmers an I prefix on a type implies that it is an interface. This is both a very common pattern and part of the .Net Design Guidelines.

Adding an I prefix for the extension method case breaks both assumptions and established guidelines.

There is also precedence for this in the Base Class Library. The majority of the extension methods available for IEnumerable are contained in the type Enumerable.

like image 154
JaredPar Avatar answered Oct 18 '22 16:10

JaredPar


I, personally, would use IThingExtensions.

From a usability standpoint, the end user never sees this class - they only include it's namespace. If the namespace is the same as IThing, then it doesn't matter - they'll already have it.

That being said, I think that the fact these are extensions for any IThing makes IThingExtensions the most clear. If you have a Thing class, calling this ThingExtensions may seem ambiguous (are you extending the interface or the implementation itself?).

That being said, the framework uses a very different approach. The framework's approach is to use a class named Thing to extend IThing. For examples, see Enumerable (extending IEnumerable) and Queryable (extending IQueryable). This would also be a very good option.

like image 22
Reed Copsey Avatar answered Oct 18 '22 16:10

Reed Copsey


Most programmers I know put all of their extension methods for an application in a static class called ExtensionMethods (or something like that), regardless of the class the extensions modify, and then they put this class into their main program namespace.

Their rationale is that, if you put the extension method in the same namespace as the class it modifies, you can confuse the method with the methods that are part of the actual class, which suggests that the extension method is part of the original functionality when it isn't.

Their isn't universal agreement on this, of course. See here: How do you manage the namespaces of your extension methods?

like image 3
Robert Harvey Avatar answered Oct 18 '22 16:10

Robert Harvey