简体   繁体   中英

Namespace & Class name collide

What is the best practice when naming a class same as the end of a namespace?

Example:

    Successful.Domain.Resource {
    public class Resource {}
    }

    Successful.Data.Resource {
    public class Resources {}
    }

If i want to use the Resource class in the Successful.Data namespace, i can't just reference Resource as i want. I need to declare its namespace like Domain.Resource.Resource

I use Resharper and structure all my code in folders, that's wye i have the namespace like that, example of folder structure:

Successful.Domain (Namespace)

  • Resource (Folder)
    • Resource.cs
    • ResourceLocation.cs
    • ResourceSettings.cs

Successful.Data (Namespace)

  • Resource (Folder)
    • Resources.cs

See:Framework Design Guidelines - Names of Namespaces

X DO NOT use the same name for a namespace and a type in that namespace.

So it is recommend to use a different class name than your namespace.

You may also see this article by Eric Lippert : Do not name a class the same as its namespace, Part One

It is not recommended to use same name for namespaces and classes.

In this case I'll name the namespace as Resources notice this is plural since namespaces provide logical grouping of types.

Class name should be more specific. What does Resource tells you? Um, nothing. Name your classes properly, class name should communicate what it does. In other words it should be self explanatory. Without knowing much about what that Recource is I can't come up with a good name.

@Habib got it right with good links so am out of choices to provide you some useful links.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM