Why is it that using namespace std;
is considered poor practice in C++, but using System;
is considered good practice in C#? They seem to be analogous (bringing standard library stuff into the global namespace).
In C#, a using
directive only impacts the file or namespace scope where it is placed.
If you include using namespace std;
in a c++ header file, it impacts not only that file, but every file that includes it. This creates conflict potential in other people's files.
You could easily argue that it's not "best practice" in C#, but the risk involved is dramatically lower than in C++ as it only impacts the file or namespace scope where the directive is placed.
Because C# has no free-standing functions.
The problem with using namespace std
in C++ is when the compiler suddenly decides to call the std::
function instead of your function which is not surprising with names like count
, distance
or find
being template functions that take just about anything.
This is much less of a problem in C# because C# does not have free-standing functions and it has stricter implicit conversions (e.g. you can't pass an int
to a function that expects a pointer).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With