What is namespace (I’m not sure if this is for all languages but I’m using C#) and what is it used for?

254 views

Searching says It is like a container to separate codes but I don’t see the benefits of it

In: 2

5 Answers

Anonymous 0 Comments

There are many, many programs. If you have a program and you program uses a variable called Leftover, that doesn’t imply that no other program anywhere can have a variable named Leftover. Each variable, in C# has limited scope, depending on where it is defined. If you use that variable in a different program, you get an error.

Anonymous 0 Comments

Lets say you are writing a code library that handles, I dunno, shapes. You put some classes in it, say, `Square`, `Circle` and `Triangle`.

Now, I create my own code library that handles musical instruments. So I put some classes there, such as `Guitar`, `Piano` and `Triangle`.

So now some other person wants to use both the libraries. But oh no, they can’t! There are two classes named `Triangle`, so the program doesn’t know which one to use.

So what do we do? We place our classes inside namespaces. For example, you can put your classes in the namespace `Leftover_cookie.Shapes`, and I’ll put my classes in `Schnutzel.Instruments`.

Now, if someone wants to use one of our classes, they have to use their fully qualified name (`Leftover_cookie.Shapes.Triangle` or `Schnutzel.Instruments.Triangle`) or use the `using` declaration to tell the compiler to strictly use one of the namespaces.

Anonymous 0 Comments

Let’s use an analogy with roads. There are many many roads called Main Street all over the US, and any town can just build a new road and call it Main Street. This is usually not a problem. If I am in Springfield, and I talk about Main Street, obviously I am talking about the Main Street in Springfield.

But what if it’s not obvious, and I need to specify which Main Street I’m talking about? Well then I can say Main Street, Springfield. That might not even be enough, because any state can create a town called Springfield. I can be really specific and say Main Street, Springfield, Massachusetts, USA, (Earth, The Sol System, Milky Way, Universe).

Here “Springfield, Massachusetts, USA” is a namespace. As long as all the road names within that town are unique, no roads will be truly indistinguishable from each other, while at the same time town planners don’t need to have a big meeting to decide all the road names all over the world.

It’s the same with computer programs. Things like variables, functions, and classes need names. We use namespaces to allow people to create useful names while making sure no two things are confused for being the same.

Namespaces are often made up using internet domain names (often reversed, like com.microsoft instead of microsoft.com). For example, if I own the domain name happy2harris.com then I will use that as a namespace to avoid “name collisions” with other people’s programs. Or maybe within that namespace I’ll create a namespace for each program I write (just like a town within a state). So I’ll have namespaces like com.happy2harris.myredditbot and com.happy2harris.billiondollaridea.

(Side note: my town in Massachusetts can’t even manage to make all the road names unique within the town. This causes a syntax error in my brain whenever I try to drive around there).

Anonymous 0 Comments

Think like addresses. You have state, city, street, street number to find a particular house.

So, Missouri.Springfield, and then the street and number.

Or, Massachusetts.Springfield.street.number.

If I’m within Springfield Missouri I just say a street and number, and I get the house I want. If I’m within Missouri I just say Springfield with house and number. But if I’m anywhere but Missouri and I want a house in the Missouri Springfield, I have to say I mean the Missouri Springfield because there are like 40 Springfields in the US.

But within C#, we list the namespaces at the beginning to specify we want to be able to use all of the programming that comes with those namespaces. You just have to give the full path of a namespace if there are any two or more namespaces that have classes with the same names. Modern Visual Studio will usually warn you if the class you just referred to exists in multiple namespaces.

Anonymous 0 Comments

Namespaces are an organization thing to keep things organized and keep class names from colliding. I can create class Processor in namespace MyCode, and that creates a class named MyCode.Processor underneath the covers. Somebody else can write NewApp.Processor without getting them confused.

You can either use the full name in your code – MyCode.Processor – or you can add “using MyCode;” to your code file. That tells the compiler that whenever you write <x> as a class name, it should also search for “MyCode.<x>”.

Related to namespaces are assemblies, which are packages for storing the actual code. It is typical but not required that the assembly have the same name as the namespace – for example, the classes in the System.Web namespace are in the assembly system.web.dll

0 views

Searching says It is like a container to separate codes but I don’t see the benefits of it

In: 2

5 Answers

Anonymous 0 Comments

There are many, many programs. If you have a program and you program uses a variable called Leftover, that doesn’t imply that no other program anywhere can have a variable named Leftover. Each variable, in C# has limited scope, depending on where it is defined. If you use that variable in a different program, you get an error.

Anonymous 0 Comments

Lets say you are writing a code library that handles, I dunno, shapes. You put some classes in it, say, `Square`, `Circle` and `Triangle`.

Now, I create my own code library that handles musical instruments. So I put some classes there, such as `Guitar`, `Piano` and `Triangle`.

So now some other person wants to use both the libraries. But oh no, they can’t! There are two classes named `Triangle`, so the program doesn’t know which one to use.

So what do we do? We place our classes inside namespaces. For example, you can put your classes in the namespace `Leftover_cookie.Shapes`, and I’ll put my classes in `Schnutzel.Instruments`.

Now, if someone wants to use one of our classes, they have to use their fully qualified name (`Leftover_cookie.Shapes.Triangle` or `Schnutzel.Instruments.Triangle`) or use the `using` declaration to tell the compiler to strictly use one of the namespaces.

Anonymous 0 Comments

Let’s use an analogy with roads. There are many many roads called Main Street all over the US, and any town can just build a new road and call it Main Street. This is usually not a problem. If I am in Springfield, and I talk about Main Street, obviously I am talking about the Main Street in Springfield.

But what if it’s not obvious, and I need to specify which Main Street I’m talking about? Well then I can say Main Street, Springfield. That might not even be enough, because any state can create a town called Springfield. I can be really specific and say Main Street, Springfield, Massachusetts, USA, (Earth, The Sol System, Milky Way, Universe).

Here “Springfield, Massachusetts, USA” is a namespace. As long as all the road names within that town are unique, no roads will be truly indistinguishable from each other, while at the same time town planners don’t need to have a big meeting to decide all the road names all over the world.

It’s the same with computer programs. Things like variables, functions, and classes need names. We use namespaces to allow people to create useful names while making sure no two things are confused for being the same.

Namespaces are often made up using internet domain names (often reversed, like com.microsoft instead of microsoft.com). For example, if I own the domain name happy2harris.com then I will use that as a namespace to avoid “name collisions” with other people’s programs. Or maybe within that namespace I’ll create a namespace for each program I write (just like a town within a state). So I’ll have namespaces like com.happy2harris.myredditbot and com.happy2harris.billiondollaridea.

(Side note: my town in Massachusetts can’t even manage to make all the road names unique within the town. This causes a syntax error in my brain whenever I try to drive around there).

Anonymous 0 Comments

Think like addresses. You have state, city, street, street number to find a particular house.

So, Missouri.Springfield, and then the street and number.

Or, Massachusetts.Springfield.street.number.

If I’m within Springfield Missouri I just say a street and number, and I get the house I want. If I’m within Missouri I just say Springfield with house and number. But if I’m anywhere but Missouri and I want a house in the Missouri Springfield, I have to say I mean the Missouri Springfield because there are like 40 Springfields in the US.

But within C#, we list the namespaces at the beginning to specify we want to be able to use all of the programming that comes with those namespaces. You just have to give the full path of a namespace if there are any two or more namespaces that have classes with the same names. Modern Visual Studio will usually warn you if the class you just referred to exists in multiple namespaces.

Anonymous 0 Comments

Namespaces are an organization thing to keep things organized and keep class names from colliding. I can create class Processor in namespace MyCode, and that creates a class named MyCode.Processor underneath the covers. Somebody else can write NewApp.Processor without getting them confused.

You can either use the full name in your code – MyCode.Processor – or you can add “using MyCode;” to your code file. That tells the compiler that whenever you write <x> as a class name, it should also search for “MyCode.<x>”.

Related to namespaces are assemblies, which are packages for storing the actual code. It is typical but not required that the assembly have the same name as the namespace – for example, the classes in the System.Web namespace are in the assembly system.web.dll