'What is the difference between the 'namespace' and the 'using' keywords in C#?

I'm new to C# and I don't fully understand what a namespace is, and what the using-keyword does. I'd appreciate it if someone could explain the difference. From my guess, using is similar to #include in C++.

If that's the case, what is a namespace for? The only exposure I've had to namespaces in C++, was when using using namespace std;, which just allowed me to forgo the std:: in my function calls.



Solution 1:[1]

The namespace-statement declares a namespace of your own; A set of "bundled classes" if you like. You can have very different classes in a name space, and you can also create sub-namespaces.

Using the namespace statement will therefor, implicitly make sure you can access other classes in the same namespace. You do not need to make explicit references within the same namespace.

Read more on namespaces in C# here.

The using directive is used to use a namespace in your code, without explicitly referencing to a namespace. This is of course useful when using a namespace more than once in a class. The directive does not change the state or status of the class, as the namespace statement does.

Read more on the using directive in C# here.

There is also much more things you can do with the using keyword...

Solution 2:[2]

Quick dirty answer -

namespace - is a collection of classes that you are naming and filling with classes yourself

using - brings in a namespace(collection of classes) to be used

For a cleaner understanding of the parts of a C# program - https://www.tutorialspoint.com/What-are-the-main-parts-of-a-Chash-program

Solution 3:[3]


you are almost correct. To find out more about namespace and using refer here
Since you are new to C# i recommend you to go through the microsoft c# and .Net documentation if you have any doubts. Hope this helps :)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 MahNas92
Solution 2 Kyle Taylor
Solution 3 Rishi Kc