Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is System.Collections a "namespace of the System namespace"?

Tags:

Okay, so I've been reading a book on C# and .NET, and while learning about the DateTime structure and the following code:

DateTime dt = new DateTime(2015, 10, 17); 

I asked myself "why didn't I have to reference it at the top by writing using System.DateTime"?

So I realized that since DateTime is a structure, and the "using" keyword is used to reference types (type being a general term to refer to a member from the set {class, interface, structure, enumeration, delegate}), defined in a particular namespace (in this case, DateTime is of type structure from the System Namespace).

But I noticed that at the top of my file, the following using directives are found (they were put there automatically when I created my C# console application):

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; 

I was wondering why the using directives that follow using System were necessary, when aren't they all under the System namespace and therefore they should already be referenced?

So I'm guessing it's because all the ones declared below it are namespaces, as opposed to a type from the set {class, interface, structure, enumeration, delegate}. But would it be wrong to refer to them as "namespaces OF (or belonging to) the namespace System," and if so, what is the reason that you must explicitly reference namespaces of a namespace instead of them being implicitly included under using System?

I hope my question was clear but if not, please let me know so I can edit it.

like image 298
FrostyStraw Avatar asked Jul 29 '16 20:07

FrostyStraw


People also ask

What is the system collections namespace?

Collections Namespace. Contains interfaces and classes that define various collections of objects, such as lists, queues, bit arrays, hash tables and dictionaries.

Why do we use System namespace in C#?

The namespace keyword is used to declare a scope that contains a set of related objects. You can use a namespace to organize code elements and to create globally unique types.


1 Answers

Internally, all .net types always have full type names;

System.DateTime System.Collections.Generic.Dictionary<string, int> System.Threading.Tasks.Task 

what the 'usings' do is to let you refer 'invisibly' to everything before a '.' character. So when you write

DateTime 

C# will try

System.DateTime System.Collections.Generic.DateTime System.Linq.DateTime System.Text.DateTime etc... 

Since only one exists (System.DateTime) it's an unambiguous match, and C# knows what you're talking about.

If there were two matches, you'd get a compiler error about the ambiguity. So if there were a System.Threading.Task.DateTime, your program wouldn't compile.

All this sets the scene for the answer to your question. If

using System; 

included everything below it, then it would be hard to ensure that things aren't ambiguous. For example, if you wanted to differentiate between two theoretical classes

System.WebPages.Button System.Desktop.Button 

then a plain old using System wouldn't play nice with new Button().

like image 72
Steve Cooper Avatar answered Oct 19 '22 15:10

Steve Cooper