Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using System; in C# vs using namespace std; in C++

Tags:

c++

c#

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).

like image 515
Demi Avatar asked Apr 17 '14 18:04

Demi


2 Answers

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.

like image 132
Reed Copsey Avatar answered Oct 27 '22 01:10

Reed Copsey


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).

like image 29
Sergey Slepov Avatar answered Oct 27 '22 01:10

Sergey Slepov