Importing Nested Namespaces Automatically in C#

Importing nested namespaces automatically in C#

C# is not Java.

A using directive is used so you don't have to type in the fully qualified name of a type. It also helps with disambiguating type names (using aliases for instance).

In the case of Console, for example, you don't need to type System.Console.

It is important to understand the difference between a namespace and an assembly - a namespace is a logical grouping of types. An assembly is a physical grouping of types. Namespaces can span assemblies.

When you reference an assembly (this is more like importing a package in Java), you gain access to all of the public types in it. In order to use a type you need to uniquely identify it. This is done through the namespace - the using directive simply means you don't have to type the fully qualified name of the type.

Nested Namespaces in C#

One way that would probably work is to using a global find and replace. What your actually trying to do is to rename two namespaces into one, and it's not going to let you.

Namespace being automatically imported

What namespace is your ASP .Net code in?

It's probably inside of HRCommon.

If you write code in a nested namespace, it will automatically import all parent namespaces (eg, code in System.Windows.Forms doesn't need to import System.Windows and System)

To prevent this, move either the library or the ASP .Net project to a different namespace.


Alternatively, you might not have namespace statements in the library.

The Default Namespace option in VS is only used to generate the namespace block in new C# source. EDIT: To clarify, the Default Namespace setting is not used by the compiler at all. It's only used by the Visual Studio to automatically insert namespace blocks in new source files.

Check the source files in the library and make sure that all of the classes are defined inside of the namespace HRCommon.

For example:

namespace HRCommon {
public class MyClass {
//...
}
}

Not able to import namespaces automatically in Visual Studio

If you have a namespace in a different DLL in your project, you first need to right-click references and select "add reference" and then checkmark the dll.

After doing this, CTRL+DOT will work :)

If it is a reference to an external package, such as NuGet, you can right-click the solution file and choose "Restore Nuget Packages"



Related Topics



Leave a reply



Submit