0

Often when including namespaces or assemblies into my code, I often run into strange cases where a namespace is inherited from another, yet classes from the parent namespace are not available. For example, when using List<> or Dictionary<>, I use the System.Collections.Generic namespace. However, if I also want to use an IEnumerator, I also have to include the System.Collections namespace. Shouldn't System.Collections already be referenced by any member of System.Collections.Generic, as it is a child? Both namespaces also share the same assembly, mscorlib.dll.

Why is the parent namespace not included when the child is?

Liam McInroy
  • 4,339
  • 5
  • 32
  • 53

1 Answers1

5

Shouldn't System.Collections already be referenced by any member of System.Collections.Generic, as it is a child?

No. There's no inheritance between namespaces. A using directive only imports types from that namespace - it doesn't import types from namespaces starting with the given namespace, or namespaces included within the given namespace's name.

For example, after:

using System.Collections;

... that doesn't let you use List<T> as a simple name for System.Collections.Generic.List<T>, nor does it let you use Guid as a simple name for System.Guid.

The only exception to this is that if you're writing code within a namespace declaration of X.Y.Z, that implicitly imports namespaces X.Y and X (as well as X.Y.Z, of course). (In that example, X.Y is the enclosing namespace if X.Y.Z, but it's not an inheritance relationship.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • So there is no actual inheritance between namespaces, Msdn just uses the term in bad context? (See [`List<>`](http://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110).aspx) for example) – Liam McInroy May 12 '14 at 16:19
  • @OutlawLemur: Where it is using the term incorrectly in that documentation? (Please quote exactly.) – Jon Skeet May 12 '14 at 16:20
  • Before the code examples or syntax in the inheritance hierarchy section – Liam McInroy May 12 '14 at 16:23
  • 3
    @OutlawLemur: That shows the inheritance of the *type* - so `System.Collections.Generic.List` inherits from `System.Object` for example. I see nothing that suggests one *namespace* inherits from another. (For example, type `Foo.Bar.Baz` could inherit from type `A.B.C`. That doesn't mean there's a relationship between the `Foo.Bar` and `A.B` namespaces.) – Jon Skeet May 12 '14 at 16:24
  • But `Foo.Bar.Baz` and `Foo.Bar` have no relationship? – Liam McInroy May 12 '14 at 16:29
  • @OutlawLemur: `Foo.Bar` is the namespace in which `Foo.Bar.Baz` is declared. It's still not an inheritance relationship though. – Jon Skeet May 12 '14 at 16:29
  • Ok... But in your example `X.Y.Z` where `X.Y.Z` imports `X.Y` and `X`... Why are many microsoft namespaces (`System.IO`, `System.Collections`) which have children not referenced in the child namespace? – Liam McInroy May 12 '14 at 16:34
  • 1
    @OutlawLemur: No, I think you misunderstood my answer. The point is that if you're *writing code* in namespace `X.Y.Z`, you can use types from `X` and `X.Y` without importing them explicitly. That has *no* impact on anyone using your type - it's only within your source code. Again, a namespace doesn't have "children" in an inheritance sense. – Jon Skeet May 12 '14 at 16:35
  • So there is a difference between C# and C++ or python haha... Thanks – Liam McInroy May 12 '14 at 16:37
  • @OutlawLemur: I don't know either C++ or Python well enough to say, I'm afraid. – Jon Skeet May 12 '14 at 16:38
  • Definitely a worthy question about clarifying the nature of namespace hierarchies in C++ vs C#, I've been struggling to articulate what the question would be but hopefully it'll appear in a new question shortly. He's using the language of inheritance but I think he may actually mean nesting, and you bring up good language too with the enclosing namespace term. Clarifying the X.Y.Z... namespace naming style and its relation to nesting namespaces I think is the key question. In the meanwhile I'll try to digest this [stuff](http://msdn.microsoft.com/en-us/library/aa691136(v=vs.71).aspx) – jxramos Dec 16 '14 at 20:40