6

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
  • I'm not sure if this will help, but have you tried `using Successful.Domain.Resource = Rsrc;`? This may help avoid name collision. I'm curious if that would work - but not sure (hence not posting it yet as answer). – LB2 May 13 '14 at 19:49

2 Answers2

10

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

ChrisW
  • 54,973
  • 13
  • 116
  • 224
Habib
  • 219,104
  • 29
  • 407
  • 436
  • Thank you Habib for that link, good explanation of the best practice. –  May 13 '14 at 19:56
  • 1
    @Habib thanks for link. Looks like that guy Eric knows C# pretty good :) – Sergey Berezovskiy May 13 '14 at 20:25
  • @OP After following Habib's advice, changing your folder structure to match your namespaces is not a bad idea, unless your application is a web app. ReSharper has a quick fix (and possibly a global fix) to do this for you. – Brian May 14 '14 at 16:47
1

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.

Community
  • 1
  • 1
Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189