Namespaces and Using Directives

If I have a namespace like:

namespace MyApp.Providers
 {
    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Globalization;
  }

Does this mean that if I create other files and classes with the same namespace, the using statements will be shared and I won’t need to include them again?

If so, isn’t that a leadership pain?

+5
source share
6 answers

No, this is only useful for a namespace section inside a file. Not for all files inside the namespace.

If you put the using statement outside the namespace, then it applies to the whole file regardless of the namespace.

He will also first search for usings inside the namespace before moving on to the outside.

+10

, , .

:

using , .

+2

, , , .

, namespace . using ?

0

. , .

: using . .

.

0

, , ; using , , .

0

. , :

:

  • 2

:

namespace MyProject.Main {
    using System;

    class Program {
        public static void Main(string[] args) {
            Console.WriteLine("Hello, World!");
        }
    }
}

// in another file:
namespace MyProject.Console {
    class Test {}
}

But if you move using System;up, compilation will fail (MyProject.Console.WriteLine does not exist).

0
source

All Articles