Click here to Skip to main content
15,883,901 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

What I have tried:

Been learning on my own using Youtube and Udemy.
Posted
Updated 10-Apr-16 23:37pm
Comments
Tomas Takac 11-Apr-16 2:35am    
Are you asking how to structure your application in terms of projects and namespaces?

It's very simple. It is a way to organise your code into distinct chunks by giving them sensible names. For example: lets say your project is called Optima.

You have your application objects in Optima.BusinessLogic namespace. Your forms might be in Optima.UI and your database access grouped in Optima.DataAccess namespace.

More, you could have library of common function in a project separate from Optima, for example MyCommonMethods.Utilities (where you write all the methods you find useful in all your code - such as extensions) or MyCommonMethods.Enums, MyCommonMethods.ApplicationAccess or some such.

This enables you to reference whole bunch of objects by using Optima.BusinessLogic or Optima.DAL without risking conflicts with existing objects either in your libraries, third party libraries or .NET.

Again an example: if you have method to convert your string to XML in MyCommonMethods called StringToXML, you could have same-named method with same parameters in Optima.BusinessLogic with totally different functionality (but specific to your Optima project).

If this helps, please accept it as a solution. Thank you.
 
Share this answer
 
v3
1st grader - not sure what age that is.
I done a bit of googling :) and assume a 5 year old.

Taking your question of namespace as a naming convention type thing.
-----
Here is a link on the more technical side - not for a 5 year old to understand but it may help you.
Names of Namespaces[^]

Or an alternative question which may also help you understand namespace.
"c# same class name different namespace"
c# - How to handle same class name in different namespaces? - Stack Overflow[^]
c# - Identical class names in different namespaces - Stack Overflow[^]

The above are a bit technical so not suited to a 5 year old.
-----
Here is one for a 11 year old if they understand how windows explorer works.

Think of a file and/or folder name as being equivalent to a ClassName (sort of)

You can have a file/folder with the same name as long as the the parent folder is different.
If you look into the system drive you should have two folders name "Program Files" and
"Program Files (x86)".

Within these two folder there are folder(s) with the same name, for example "Windows NT".
If we asked the 11 year old to delete the "Windows NT" folder :), which one would they delete?

To be more specific we could put the folder names into NameSpace type notation.
"Program Files"."Windows NT"
"Program Files (x86)"."Windows NT"

So if we asked them to delete "Program Files (x86)"."Windows NT", they would know which one to delete.
------
Okay here is one for a 5 year old.

Lets say the 1st grader was in school and Kevin was their best friend.
But there where two Kevin's in the same class (lets us assume best friend Kevin is in the same class also).

So you ask them which Kevin?
Do you know the boys surname?

And they say "Kevin Costner is my best friend not Kevin Bacon".

Well you can use NameSpace notation on these names too.
Costner.Kevin
and
Bacon.Kevin

So now you can ascertain which one to invite over for a tea date.
-----

Hope that helps you out and I did't get the wrong end of the stick :)
 
Share this answer
 
Name spaces exist just to avoid name clashes. Suppose, for instance, you define an object a class called File. Such a name is already used by the .NET Framework and there would be a collision. However, since the .NET File actually belongs to the System.IO namespace, its fully qualified name is System.IO.File and there is no name clash.
 
Share this answer
 
v3
Comments
Richard MacCutchan 11-Apr-16 3:19am    
I think you mean "define a class".
CPallini 11-Apr-16 4:49am    
You think right. Thank you Richard.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900