0
namespace College
{
    namespace Lib
        {
            class Book
        {
            public void Issue()
            {
                // Implementation code
            }
        }
            class Journal
        {
            public void Issue()
            {
                // Implementation code
            }
        }
    }
}

Now to use Issue() method of class Book in a different namespace, the following two approaches work.

  1. College.Lib.Book b = new College.Lib.Book(); b.Issue();

  2. using College.Lib; Book b = new Book(); b.Issue();

And the following two approaches don't work.

i. using College; Lib.Book b = new Lib.Book(); b.Issue();

ii. using College.Lib.Book; Book b = new Book(); b.Issue();

Why don't the last two codes work?

Ben Randall
  • 1,205
  • 10
  • 27

1 Answers1

1

In the first case, the original designers of C# decided that a using directive should bring the types in a namespace into scope, so to speak, but not bring the namespaces in a given namespace into scope. It was felt that "using" means "I have a bunch of types I want to use" and not "I have a bunch of sub-namespaces I want to use".

In the second case: the feature of "using" a type was added to C# 6. It brings the static members of the type "into scope". Perhaps you are using an older version of C#?

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
  • Just a clarification about the second case, in order to 'using' a type, you actually need to use the statement `using static MyType;` in order to pull in the static members (see [this post](https://stackoverflow.com/questions/31852389/how-do-i-use-the-c6-using-static-feature)). It seems to me like the second statement just doesn't work because you can only use `using` statements to import namespaces. – Ben Randall Feb 27 '16 at 07:16
  • @BenRandall: Thanks, I should have clarified that. – Eric Lippert Feb 27 '16 at 14:09