Help understanding the whole class name space thing.

earleybird36

New member
Joined
Apr 11, 2016
Messages
1
Programming Experience
Beginner
Hi all, Just changing careers and learning C#. I've played around with C, and Swift and know the basics. C# is ok so far but whats holing me back is the name space section. I just can't wrap my head around it. Can somebody here explain it to me like a 1st grader. I watched many youtube videos and I know the "definition" of them, but what are they in terms of the big picture? How to actually use them in a real complete program? Anyone willing to help is greatly appreciated.
 
It's pretty simple stuff really. Namespaces are a logical space within which all type names are unique.

Firstly, namespaces are logical, not physical. For instance, the System.Windows.Forms namespace doesn't exist anywhere specific, unlike the System.Windows.Forms.dll assembly. The assembly is a physical file that the code for a type resides in. The namespace is just a conceptual way to categorise types.

If you're creating a Windows Forms application then you immediately know that you're going to find the controls you want in the System.Windows.Forms namespace. That makes your life easier by immediately filtering out many thousands of irrelevant types. Inside that namespace there is a TextBox class that encapsulates the TextBox control you see on your form. If you were creating an ASP.NET Web Forms application then you would be looking for the TextBox class in the System.Web.UI.WebControls namespace. Those two types can have the same short name because their fully qualified names are different. Within the context of each application they can be referred to just as TextBox, which is logical and intuitive, but if there were no namespaces then they'd need a longer name in order to be differentiable.

So, namespaces let you categorise types and also use short, intuitive names that may be the same for multiple types.
 

Latest posts

Back
Top Bottom