C# C# Basics (Retired) Console I/O Namespaces

<noob />
<noob />
16,316 Points

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?:

namespace TreeHouse.FitnessFrog
  1. 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?

thanks!

4 Answers

Kevin Gates
Kevin Gates
14,701 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.)

Both up and 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 Human.Movement.Straight().

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 Straight, Merge, Start, 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 Play_Radio, 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 Car.Auxiliary_Functions.PlayRadio() or Car.Movement.Straight().

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:

using Human;

Now, your program knows if you use the shorthand of Movement.Straight() without the namespace you really mean Human.Movement.Straight().

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 Car.Movement.Straight();

I know this was a long explanation but I hope you found it helpful.

<noob />
<noob />
16,316 Points

thanks for the answer. i find the c# object course to be super difficult to follow even though i have experience with java, can u point me to other course about objects in c#? btw as a c# student u recommend this language more than java?

Kevin Gates
Kevin Gates
14,701 Points

Hi noob developer , it can be confusing at first. I would recommend rewatching certain parts. A few of the teachers in the C# and ASP.NET are not my favorite, but others are really great.

I don't have experience with Java, but haven't read a lot of comparisons recently, the benefit of C# is it's wide usage, the ability to have it be on the backend for website, as well as on the front-end, in part via "Razor Pages", but also an upcoming (still experimental) "Blazor" page, where C# (via WebAssembly) can have C# run on the front-end of a website instead of frameworks like Angular, Vue, React, etc.

Additionally, C# can be used in gaming, now with .Net Core can be used on any server whether windows, mac, linux, etc.

For it is quite flexible.

It is my opinion that they need to refresh the C# courses here. However, their information from my perspective has been the overall best coverage of the information at a good pace. The only other one I've done which was helpful was this: https://app.pluralsight.com/library/courses/csharp-fundamentals-csharp5 , but it was finished in 2014. So while helpful the language has evolved some in the past 5 years.

Steven Parker
Steven Parker
173,876 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 Mehta
Akshay Mehta
1,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.

<noob />
<noob />
16,316 Points

I still dont fully understand, if your example you said that if we a walking class we can access the class by usuing the "TreeHouse.FitnessFrog.walk.", How is that work?

In our program "namespace TreeHouse.FitnessFrog" isnt doing anything

Akshay Mehta
Akshay Mehta
1,831 Points

I apologize, my explanation above was very bad 😂. So namespaces are basically only used so that functions can have the same name but in two different namespaces. So let’s say you have a function called walk and it is in the namespace treehouse.fitnessFrog.Walk. You can have a different namespace called human with a class Bob and he can also have a function walk. The only difference is to call the fitness frog one you type “treehouse.fitnessFrog.Walk()” but to call the other one you write “Human.bob.Walk()”. To conclude they are two different functions that do different things but they have the same name due to the namespace change.

<noob />
<noob />
16,316 Points

Kevin Gates hi kevin, u recommend any other courses in pluralsight ?, whixh courses here are good in ur opnion? why is missing?, why u chose c# over java?

Kevin Gates
Kevin Gates
14,701 Points

By and large, Treehouse is better than Pluralsight in my opinion. I mentioned it only because I started there and that on particular course was okay. But Treehouse builds on itself and is dramatically better paced.

C# vs Java: it depends on your goals. Here's one of many comparisons: https://dignitas.digital/blog/java-vs-csharp/