1

Scenario:

Assume I want to define my own class.

public class Person { } 

I wish to put it in a namespace System.

Note: I have not included the directive, 'using System' at the top..

namespace System { public class Person { public void Display() { Console.WriteLine("I am mine"); } } } 

Though I have not included using System; directive at the top, I am still able to access System.Console.WriteLine in my method, since my declared namespace is System.

How is this possible? how does it go about?

6
  • mscorlib.dll is available automatically. So, even if you don't add "using System;" it is available to your class. Commented Jun 10, 2009 at 6:32
  • @shahkalpesh that's not the case. The only types you can use without a specific 'using' statement are the C# alias types such as int/string/object/etc Commented Jun 10, 2009 at 6:37
  • That's not true either, as this works: global::System.Console.WriteLine("Testing, 1, 2, 3..."); Commented Jun 10, 2009 at 6:40
  • Note that using directives are to do with namespaces, whereas mscorlib.dll is an assembly. They're very different concepts. Commented Jun 10, 2009 at 6:44
  • @Jon: You are right. However System namespace & Console class is part of mscorlib.dll. And that being the base assembly (containing Object), it is available by default. Am I wrong in saying that? Commented Jun 10, 2009 at 22:21

6 Answers 6

8

If you declare namespace Foo.Bar.Baz, every namespace in that hierarchy (Foo, Foo.Bar and Foo.Bar.Baz) is searched when you reference a type from within that namespace declaration:

namespace Foo.Bar.Baz { class Test { static void Main() { // This will search for Foo.Bar.Baz.SomeType, // Foo.Bar.SomeType, Foo.SomeType, SomeType, // in that order SomeType.StaticMethod(); } } } 

See section 3.8 of the C# 3.0 language specification for the gory details.

However, I hope you are not seriously considering using this as a way to avoid adding using directives. Creating your own types in the System namespace hierarchy is a very bad idea.

Sign up to request clarification or add additional context in comments.

Comments

1

You don't need the using directive inside the same namespace.

If you declare your classes in System then "using System;" is implicit.

However, this is probably not where you should be putting your classes.

Comments

1

Because you have defined your type in the System namespace, you have access to all other types defined in the System namespace.

Similarly, if you defined your class in System.Data, you would have access to System and System.Data without requiring explicit using statements.

Namespaces may be duplicated across assemblies.

Note that defining your type in the same namespace does not grant you access to internal members of types defined in the same namespace but different assembly.

Comments

0

Anything declared inside a namespace has access to all the other classes in that same namespace (provided you have a reference to the assembly containing that namespace and those classes).

Comments

0

Namespaces implicitly have public access and this is not modifiable.

Comments

0

This is because you have declared your class as being part of the namespace and therefore it automatically includes the assembly for it.

In the same way any file within a project namespace doesn't need to declare it as using that project namespace.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.