C# Class naming convention: Is it BaseClass or ClassBase or AbstractClass

C#Naming ConventionsBase Class

C# Problem Overview


What is the recommended approach to naming base classes? Is it prefixing the type name with "Base" or "Abstract" or would we just suffix it with "Base"?

Consider the following:

type: ViewModel e.g. MainViewModel, ReportViewModel

base class: BaseViewModel or ViewModelBase or AbstractViewModel

Also consider:

type: Product e.g. VirtualProduct, ExpiringProduct

base class: BaseProduct or ProductBase or AbstractProduct

Which do you think is more standard?

class Entity : EntityBase
{
}

or

class Entity : BaseEntity
{
}

C# Solutions


Solution 1 - C#

There are examples in the Framework with the Base suffix, e.g. System.Configuration.Provider.ProviderBase, System.Web.SessionState.SessionStateStoreProviderBase.

But by no means all abstract base classes in the Framework follow this convention (e.g. System.Data.Common.DbParameter, System.Data.Common.DbCommand).

Personally I would avoid using the suffix unless I wanted to emphasize the fact that it's an abstract class and felt that otherwise users of the class might expect the name to indicate a concrete implementation.

Solution 2 - C#

None of the above. Consider what purpose your base class provides; name it that. For example, the base class of Automobile and Bicycle could be Vehicle.

If you're creating base classes just to have a base class of one class, and with no purpose or reason other than that, you're probably doing something wrong.

Solution 3 - C#

If you're talking about virtual base classes, Microsoft's standard is ClassnameBase (like CollectionBase.)

Solution 4 - C#

We use BaseEntity, but I think it your own preference. I frequently see the other.

Just be consistent within your context, be that your project, namespace or if possible, your team. Different conventions are worse than a bad convention IMHO.

Solution 5 - C#

I think its a matter of choice. I'd say if you are creating a lot of base classes then perhaps it is better to go with BaseClassname always because that way you can ALWAYS find out what base classes you can start using by just typing Base and getting the rest of the help from Intellisense. What if you had 20 Base classes and you added Base as suffix and you forgot what was the name of the base class? Do you want to create a class diagram first from VS and find out what base classes are available? It's alright to name them ClassBase when it is just one or two classes.

Same goes for decision between GetItems and ItemsGet function. I'd say for readability's sake atleast - go for GetItems. Follow the conventions :)

Solution 6 - C#

Personally, I would recommend against adding the word base at all. You never know when you'll have to change the code around and it won't be the base object anymore. That being said, we have done this in the past, we prefixed the word Base on the front. It seems to flow better.

Solution 7 - C#

BaseEntity looks a lot like camel case - strName, bseEntity. I'd go for EntityBase since it defines the subject first, which will help you identify it's function quicker.

Solution 8 - C#

Always think about alphabetizing when you name stuff. I really don't like looking at a SQL server and every stored procedure is named usp[something]. Along the same lines, don't overuse Get and Set as leading names for a function. Instead of GetItems or PlaceOrder, think about naming them as ItemsGet or OrderPlace.

So, in general, ClassnameBase / EntityBase would be a better choice.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSoni AliView Question on Stackoverflow
Solution 1 - C#JoeView Answer on Stackoverflow
Solution 2 - C#Paul SonierView Answer on Stackoverflow
Solution 3 - C#Yes - that Jake.View Answer on Stackoverflow
Solution 4 - C#Gary.RayView Answer on Stackoverflow
Solution 5 - C#AbhishekView Answer on Stackoverflow
Solution 6 - C#kemiller2002View Answer on Stackoverflow
Solution 7 - C#Mike RobinsonView Answer on Stackoverflow
Solution 8 - C#marccView Answer on Stackoverflow