d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > C# Namespaces
Add Reply New Topic New Poll
Member
Posts: 34,587
Joined: Mar 25 2009
Gold: 12,633.00
Aug 19 2015 12:10am
I've looked at multiple tutorials + the book i'm reading and i still don't full understand what namespaces are and what they're used for.....


From my understanding, namespaces are used as a container to hold names for classes, that might use the same name? idk.....




Would appreciate if someone could go very indepth about what they are and what they're for.... the tutorial and book i'm using didn't say much
Member
Posts: 2,832
Joined: Aug 17 2015
Gold: 1.00
Aug 19 2015 12:59am
Its not a big deal, here we go:

Quote

#include <iostream>
using namespace std;

namespace ferf-one
{
int d2 = 1;
int jsp = 2;
}

namespace ferf-two
{
int d2 = 3;
int jsp = 4;
}

int main () {
using ferf-one::d2;
using ferf-two::jsp;
cout << d2 << endl;
cout << jsp << endl;
cout << ferf-one::jsp << endl;
cout << ferf-two::d2 << endl;
}

return 0;

Output:
1
4
2
3


so, as u can see the namespace (as the name namespace itself says) declared a special room for variable names
you can declare multiple namespaces and use them by "using namespace ferf-one" (whole room) or "ferf-one::jsp" (single variable)
namespaces can be used in complex structures as well
Member
Posts: 34,587
Joined: Mar 25 2009
Gold: 12,633.00
Aug 19 2015 01:04am
Quote (TragischerEinzelfall @ Aug 19 2015 02:59am)
Its not a big deal, here we go:



so, as u can see the namespace (as the name namespace itself says) declared a special room for variable names
you can declare multiple namespaces and use them by "using namespace ferf-one" (whole room) or "ferf-one::jsp" (single variable)
namespaces can be used in complex structures as well


hmmm thanks, that bettered my understanding a bit :)



so they're basically to hold anything with the same name, variables, classes, etc....Sounds kind of dumb, why not just use a different name
Member
Posts: 2,832
Joined: Aug 17 2015
Gold: 1.00
Aug 19 2015 01:16am
Quote (ferf @ Aug 19 2015 09:04am)
hmmm thanks, that bettered my understanding a bit :)



so they're basically to hold anything with the same name, variables, classes, etc....Sounds kind of dumb, why not just use a different name


first of all im new to c++ as well, so maybe there are mistakes in my knowledge.

till now i used only one time activ namespaces in a project. but normally you use it in a indirect way.
nearly every method that you callup in your projects use namespaces. if they wont you could have a problem...

for example your using a method to give something out on a display and this method use the variable "n"

"n" is used in many programs and these method would overwrite your variable "n" if it wouldnt use a another namespace.

This post was edited by TragischerEinzelfall on Aug 19 2015 01:16am
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Aug 19 2015 06:46am
Quote (ferf @ Aug 19 2015 03:04am)
hmmm thanks, that bettered my understanding a bit :)



so they're basically to hold anything with the same name, variables, classes, etc....Sounds kind of dumb, why not just use a different name


you're absolutely right. before you write any code, you should call the thousands of C# developers out there and ask what their classes are named so that you can pick a different name.
Member
Posts: 15,717
Joined: Aug 20 2007
Gold: 481.00
Aug 19 2015 07:26am
i was going to tell you what they were used for, had it all typed out

but then i realized just how stupid this question really is
Member
Posts: 5,880
Joined: Sep 8 2007
Gold: 17,541.00
Nov 19 2015 05:57pm
Quote (t9x @ 19 Aug 2015 03:26)
i was going to tell you what they were used for, had it all typed out

but then i realized just how stupid this question really is


This is one of the most common issues beginners learning .NET run into. This is far from a stupid question.

I will say it's quite a lazy question though. They're literally hundreds of resources online that can better answer this question.

@OP If i can give you one piece of advise, It's... Google is your best friend when it comes to learning.

On Topic - I personally hate learning from books. I'm a visual learner and i prefer learning from example. With that said, here is a site that has links to 200 SHORT (youtube) C# tutorials for entry level. Video #19 touches on Namespaces but i would suggest you start watching from #1.

https://www.thenewboston.com/videos.php?cat=15
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll