4

I have namespaces:

MyProject.Core.Db
MyProject.Core.Model

And I have classes:

MyProject.Core.Db.User
MyProject.Core.Model.User

Is it possible something like:

using MyProject.Core;

namespace MyProject.BLL
{
    public class Logic
    {
        public static void DoSomething()
        {
            var userEntity = new Db.User();
            var userModel = new Model.User();
        }
    }
}

I just want to avoid using suffixes in class names (UserModel, UserEntity).

Is it possible to do in somehow in C#?

4
  • 1
    yes in VB.NET :) In C# you cannot write IO.File.Delete();
    – albert
    Jul 3, 2013 at 13:31
  • VB.NET supports this, C# doesn't. Clearly it is important that you tag your question appropriately. Jul 3, 2013 at 13:43
  • Surely it is possible? I just put an example below. The question reads "I just want to avoid using suffixes in class names" and mentions new Db.User() (not new User()) etc. He just needs to specify the namespace one level up.
    – andreister
    Jul 3, 2013 at 14:01
  • @andreister He wants to add a using for the entirety of the common portion of the namespaces, while only specifying the namespaces that are specific to those classes.
    – Servy
    Jul 3, 2013 at 14:04

5 Answers 5

4

I don't understand why people say it's not possible. Surely it is possible, you just need to be a bit more specific in the namespaces when you create the target classes (ie you can omit only the common part of the namespace):

namespace MyProject.Core.Db
{
    public class User
    {
    }
}

namespace MyProject.Core.Model
{
    public class User
    {
    }
}

namespace MyProject.BLL
{
    public class Logic
    {
        public static void DoSomething()
        {
            var foo = new Core.Db.User();
            var boo = new Core.Model.User();
        }
    }
}

The way you're avoiding a fully qualified name within BLL is by being inside of a common namespace with the other two.

5
  • 1
    I think he wants Foo.User instead of Server.Foo.User. Jul 3, 2013 at 13:57
  • No, he said "I just want to avoid using suffixes in class names" - the above solves the problem.
    – andreister
    Jul 3, 2013 at 13:58
  • 1
    @andreister I think you're mistaken. The actual question is "Is it possible something like this? * code sample using Foo.User instead of Server.Foo.User*. Jul 3, 2013 at 14:03
  • See I updated namespaces to be the same as in the question. The difference between my answer's code and his question's code is Core prefix - and now the code compiles and solves the OP immediate problem. That's the closest one can get. Other people are just saying "it's not possible" in general case. That's the only reason why I chimed in - becase OP "just wants to avoid using suffixes in class names" and I believe it's a good wish :)
    – andreister
    Jul 3, 2013 at 14:09
  • @andreister There are lots of ways of doing this without changing the class names. No matter what, it's always an option to just use the fully qualified name, although in some cases you may need the global:: prefix to disambiguate. What's not possible is to do exactly what the OP is specifying and use a using to bring a namespace into scope. That isn't possible.
    – Servy
    Jul 3, 2013 at 14:18
4

What you're trying to achieve is not possible. The closest thing you will get is a using alias directive which looks like this:

using User = Myproject.Core.Db.User;

This will remove the need to fully qualify the path for Myproject.Core.Db.User. You will still need to specify the fully qualified path for at least one of the classes, though. You could create another alias for the other type as Servy demonstrated but at this point I would just rename the classes.

I think the real solution here is to give your classes more descriptive identifiers.

2
  • If you gave it any alias other that User you'd be able to add a using for one namespace, and then use the alias for the other. Of all of the aliases to pick, this is the only one that's not useful. Also note the situation isn't always avoidable by changing names, for example there are a number of classes in the BCL that share a name in different namespaces, such as Timer (which has quite a few).
    – Servy
    Jul 3, 2013 at 13:51
  • @Servy Tbh that didn't occur to me, so thanks. But regardless, he might as well just give the classes those identifiers in which case. Jul 3, 2013 at 13:55
2

C# does support relative namespace references.

In your case, that means if you're in the namespace MyProject.Core, you can references your classes as Db.User and Model.User. But if you're in the namespace MyProject.BLL, you have to include the Core prefix (Core.Db.User and Core.Model.User).

If that's not good enough for your and you don't want to change your namespace structure, your best choice is probably to add usings to all files that use the types in question.

using DbUser = MyProject.Core.Db.User;
using ModelUser = MyProject.Core.Model.User;
2

One thing you can do, and we probably should do a lot more, is to specify usings relative to the current namespace. To do this, just move your usings inside the namespace declaration. It doesn't fix your stated problem, but the shorter relative paths are less brittle and your project will be easier to refactor.

namespace MyProject.Core{
    using Db;
    using Model;
0

You can add an alias for the one class that you don't import the namespace of:

using MyProject.Core.Db;
using ModelUser = MyProject.Core.Model.User;

namespace MyProject.BLL
{
    public class Logic
    {
        public static void DoSomething()
        {
            var userEntity = new User();
            var userModel = new ModelUser();
        }
    }
}

In C# it's not possible to use the example that's shown; it's simply not a supported feature.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.