Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When same-named namespaces exist (in current scope), how to refer any of them?

Tags:

c#

I have:

namespace Book {
   ...
}

    ...
    ...



namespace Company 
{ 
    public class Book {

    }
    ...
    ...
    ...
    ...
    ...
    ...
    public class MyBook : Book.smth
    {
    }
}

How I make it, as MyBook to inherit the top-level Book namespace, instead of Company's ?

like image 686
T.Todua Avatar asked Mar 23 '18 22:03

T.Todua


People also ask

Can namespace and class have same name?

Inside a namespace, no two classes can have the same name.

How do I access variables from namespace?

The namespaces in some namespaces may also be nested. To access them we are required to use the scope resolution operator :: operator the number of times that are there to access them. When we want to access the variable sample , we need to use example1::example2::example3::sample .

What is namespace scope C++?

A namespace is a declarative region that provides a scope to the identifiers (the names of types, functions, variables, etc) inside it. Namespaces are used to organize code into logical groups and to prevent name collisions that can occur especially when your code base includes multiple libraries.

What is global namespace C++?

Beginning of C++ only. Global scope or global namespace scope is the outermost namespace scope of a program, in which objects, functions, types and templates can be defined. A name has global namespace scope if the identifier's declaration appears outside of all blocks, namespaces, and classes.


1 Answers

First off, ideally you do not get into this bad situation in the first place. Try to name your namespaces and classes so that they don't conflict with those of your dependencies! But sometimes it is unavoidable, particularly when a dependency adds a feature that you already implemented.


Technique #1:

You can do that with the global:: modifier.

namespace Company 
{ 
  namespace Xyz 
  { 
    public class DefaultClass {} 
  }
  public class myClass: global::Xyz.DefaultClass

Now Xyz.DefaultClass refers to the top level Xyz.DefaultClass, not Company.Xyz.DefaultClass


Technique #2:

Use a using alias directive, either on the namespace:

using TheOtherXyz = Xyz;
namespace Company {
  namespace Xyz 
  { 
    public class DefaultClass {} 
  }
  public class MyClass : TheOtherXyz.DefaultClass

or on the class:

using TheOtherDefault = Xyz.DefaultClass;
namespace Company {
  namespace Xyz 
  { 
    public class DefaultClass {} 
  }
  public class MyClass : TheOtherDefault

Note that the second variation does not work on generic classes. You can say

using StringList = System.Collections.Generic.List<String>

but you cannot say

using MyList<T> = System.Collections.Generic.List<T>

to make a generic alias. It's a perfectly sensible feature, it's just never been high priority enough to add it.


A question you didn't ask, but I'll answer it anyways:

What if I have namespace N with class C in two different assemblies that I am referencing? N.C is now ambiguous and global:: doesn't help, and neither does a using alias directive.

In that unfortunate case you use an extern alias directive, which you can read about here.

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/extern-alias


If you are interested in other things that can go horribly wrong when you have a name collision like this, see my articles on that subject here:

https://blogs.msdn.microsoft.com/ericlippert/tag/namespaces/

like image 73
Eric Lippert Avatar answered Dec 15 '22 00:12

Eric Lippert