System.Console
Hi, Is good practice to use System.Console.WriteLine(); instead of declaring the System library outside of main?
10 Replies
no
using System;
is your friendI see no reason why you would define the whole namespace of an object. It makes it very unreadable.
The only real reason would be ambiguity between two classes, in which case you'd be able to define an alias or define specifically what class to use at the top with an
using
statement. Always at the top of the file.using namespace std; in C++ can cause some bugs later on the track that way its best practice to not use namespace std; but to use std::cout<<"Test"; or std::cin >> test;
Interesting. There's no bugs in C# with this though
very good
Maybe because they named it
cout
which could be unspecific?
Isn't std
a class type in C++ though?
Like here, we have Console
You should stick those before a method, though. It's required anyway unless you put using static System.Console;
at the top of the fileyes it has objects like cout for output, cin for input, cerr for errors & clog for logs
I guess I'm starting like C# more than C++
I can't comment on C++ compared to C# but I know it's not going to introduce memory leaks or vulnerabilities as quickly 😄
nice
That's because std pulls in a crap ton of symbols. And because variable names cause conflicts with type names.
In C#, you can name a property the same as another class, it will work without an issue
And in C# everything is in classes, there are no free functions
C++ has namespaces