2

I'm working on a C# library (let's just call it "Foo" for the sake of this question). It has some needs very similar to standard .NET needs: for example, it provides some drawing services, and some conversion services.

For the sake of familiarity and users of the library being able to guess what things are called, I'd like to follow the .NET standard, and name these parts of the library Foo.Drawing and Foo.Convert (and so on). But I'm finding that in actual use, this causes pain. People almost always have "using System;" at the top of each file, and when using this library, they want to have "using Foo;" as well. But now they have two Drawing and two Convert modules, and hilarity ensues.

For example, now instead of just using Drawing.Color for a parameter or variable type, you have to explicitly spell out System.Drawing.Color, or the compiler complains that Foo.Drawing doesn't have a Color type. Similarly, you want to use a standard Convert.ToInt32, you have to say System.Convert.ToInt32, even though you're already using System, because otherwise it finds Foo.Convert and fails to find ToInt32.

I understand why all this is as it is, but I'm still new to the C# community, so I don't know which is the most standard solution:

  1. Leave it this way, and expect users to use fully-qualified names where necessary?
  2. Rename the conflicting modules to something else (maybe Foo.Graphics instead of Foo.Drawing, and Foo.Conversion instead of Foo.Convert)?
  3. Use some prefix on the standard names (Foo.FDrawing and Foo.FConvert)?
  4. Something else?

Any advice from you more experienced C# gurus will be appreciated!

Joe Strout
  • 2,634
  • 2
  • 28
  • 39
  • 1
    It's not clear to me what you mean - is `Foo.Drawing` another namespace, or is it a type? I would definitely try to avoid coming up with type names which clash with public types in `System`... – Jon Skeet Jun 02 '14 at 14:16
  • I vote one namespace per library. – Sinatr Jun 02 '14 at 14:22
  • Why is `Drawing` an issue? There's not a `System.Drawing` class. – D Stanley Jun 02 '14 at 14:29
  • There's a System.Drawing namespace (http://msdn.microsoft.com/en-us/library/system.drawing.aspx), and a Foo.Drawing namespace, which is one of several such clashes that make using Foo more annoying than it ought to be. – Joe Strout Jun 03 '14 at 15:09
  • Wait, no, that's not quite right. Foo.Drawing is a class. System.Drawing is a namespace, not a class, but I don't see how that helps -- I still get conflicts when I'm in the Foo namespace and try to use (say) Drawing.Color, because the compiler appears to be looking for Foo.Drawing.Color, which doesn't exist. – Joe Strout Jun 03 '14 at 15:14

2 Answers2

1

You can use namespace aliasing :

using System;
using FConvert = Foo.Convert;

public class Bar
{
     public void Test()
     {
          var a = Convert.ToInt32("1");
          var b = FConvert.ToInt32("1");
     }
}
Xiaoy312
  • 14,292
  • 1
  • 32
  • 44
0

One of the main usage of namespaces is to avoid name clashing.

It means that namespaces allow developers to create types with identical names, as long as the belong to different namespaces.

A library usually have at least a root namespace, and possibly nested namespaces that logically groups the related types.

Name your types as you wish, as long as the names are meaningful and represent what the type really are. A client of your library expects a type named Animal to represent an Animal, not something else. The same applies for naming namespaces.

However, avoid at all cost the names from System, since it will be really annoying for your library clients (as you described) to deal with conflicting names all over the place.

A common way to deal with conflicting namesapces inside a class is to use namespace aliasing:

 using FooConvert = Foo.Convert;
 using BarConvert = Bar.Convert;
quantdev
  • 23,517
  • 5
  • 55
  • 88
  • OK, I appreciate the time you've taken to write up such a nice explanation, but I don't really feel any clearer as to what to do. Your "avoid at all cost the names from System" advice agrees with my own intuition... but what then should I do? Let's take Convert for example; surely it's not uncommon for a library to have types that need converting. Should I name this Convert and let people use namespace aliasing (or fully-qualified names) to deal with it, or name it something else to avoid the conflict? – Joe Strout Jun 03 '14 at 15:22