Use of the system; in C # vs using the std namespace; in c ++

Why does this mean using namespace std; considered bad practice in C ++, but using System; considered good practice in c #? They seem to be similar (bring standard libraries to the global namespace).

+7
c ++ c #
source share
2 answers

Since C # does not have independent functions.

The problem with using namespace std in C ++ is that the compiler suddenly decides to call the std:: function instead of your function, which is not surprising since names like count , distance or find are template functions that take anything you want.

This is much less of a problem in C #, since C # has no free-standing functions and has more strict implicit conversions (for example, you cannot pass an int function that expects a pointer).

+1
source share

In C #, the using directive only affects the area of โ€‹โ€‹the file or namespace in which it is located.

If you enable using namespace std; in the C ++ header file, this affects not only this file, but also each of its files. This creates conflicting potential in other people's files.

You can easily say that this is not a โ€œbest practiceโ€ in C #, but the risk associated with it is much lower than in C ++, since it only affects the content area or namespace in which the directive is located.

+17
source share

All Articles