some questions about what is the use of namespaces
hi, 1.i still dont fully understand what is the use of the namespaces
for example what this line exactly does?:
- i understand that if we type "using System" c sharp let us to stop add "system" into our console.readLine etc.. , "using" System is an example of a name space or its something else?
Kevin Gates14,814 Points
Hi there noob developer , I will take a stab at answering this question as this is my second time through this course.
By way of example imagine that every method is a skill you can do in real life. For instance, the
up method could be standing up by default. Additionally, a
straight method could be how you walk forward. (Don't worry, I am using "Up" and "Straight" on purpose.)
straight are normal functions of a person's movement. So we could bundle those together in a class called Movement.
However, there are other things you can do like you have a
smell function for smelling things and a
taste function for tasting things. We may put those methods under a class called Senses.
No, for you, you have the ability to move ("Movement") and to sense things around you ("Senses"). These are a natural part of who you are. Therefore, we may have all of these classes (with their methods) under a Namespace of Human.
Back to C# world, if you are walking straight, then a way to write this is
Now, while is this helpful. Well, as you get older, you may get a Driver's License to drive a car.
With cars you have various methods like
Stop, etc. Those methods act on what the car does when moving, so we could group those methods under a class called Movement (are you seeing where I am going with this?).
But cars can do other things too, they have auxiliary functions like
Charge_Phone, etc. These can be under a class of Auxiliary_Functions. So how will we put together a car's Movement class and Auxiliary Functions class? We could add them to a namespace called Car. So in C# you could have methods like
Okay, finally, let's bring it together.
You have a program. If you just put a class and it's method of:
Movement.Straight(), am I talking about moving your human body or moving the car? You don't know. Neither does the computer.
Therefore, having the namespace gives clarity for similarly named (but different) classes and functions.
Human.Movement.Straight() is super clear.
Now, further more, let's say you have a long program about what a person does throughout the day. You have the person doing a lot. So instead of having to always write
Human.Movement.Straight() you say at the top:
Now, your program knows if you use the shorthand of
Movement.Straight() without the namespace you really mean
If you ever want to person to drive a car, then you differentiate it from your shorthand way of typing by adding that namespace, so
I know this was a long explanation but I hope you found it helpful.
Steven Parker205,566 Points
The "namespace" is essentially a way of grouping things together. Anything defined in a specific namespace has access to everything else defined in the same namespace.
The "using" statement gives you access to the things defined in a different namespace. There are many common ones, such as "System" that you might use in building a program.
Akshay Mehta1,831 Points
How I understood namespace is, think of a namespace as one huge group. Inside of that group is different parts and different actions. So to put this in an easier way, In that line of code, you said there is a namespace called TreeHouse.FitnessFrog. Inside of this namespace, there can be different parts like let's say a walking class and a running class. As a namespace, if you wanted to access these you would simply put TreeHouse.FitnessFrog.walk. It is basically a way of grouping things so that you can have a function named the same thing but in different namespaces. I hope this isn't confusing if you still don't understand feel free to ask more.