Reputation: 17454
I have years of experience in C++ and Java, but I am new to C#.
Recently when I was reading a book on C#, they address the following as name spaces, which I found it wierd. According to my knowledge in C++, these are actually pre-processor directives
.
using System; //A book called these as namespace
using System.Collections.Generic; //A book called these as namespace
using System.Text; //A book called these as namespace
And according to the msdn website, the above are known as directives
.
If the above is call namespace
What about the following?
namespace My_First_Program //This is also call namespace
{
class Program
{
static void Main()
}
}
So my first question is: Which exactly is the namespace? Am I correct to call using System;
as pre-processor directive
?
My second question is: Can I assumenamespace My_First_Program
works similar to using namespace std
in C++ ?
Upvotes: 2
Views: 409
Reputation: 107247
No, C#'s using
is not a preprocessor directive.
In C#, these are both almost identical to C++
using System;
would have similar intent to
using namespace std;
or in Java
import java.util.*;
viz that symbols in the System
namespace are now in scope, provided that the requisite assemblies are also referenced by this project. Without using
, all code which access System
would need to qualify the full namespace as a prefix, e.g. System.DateTime
. You can also alias a namespace with using
, e.g. if the result of adding multiple using
statements brings 2 or more symbols with the same name into scope, e.g. (using m=My.Very.Long.NameSpace
)
Similarly:
namespace foo
{
}
also has the same effect as it does in C++
- this places all symbols defined in the block as in the namespace provided, foo
. As with other languages, this is done to prevent cluttering the global namespace. This is quite similar to the package
keyword in Java files. In theory in C#
you can have multiple namespace
blocks in a file, although this would be unusual and likely poor practice to do so.
Upvotes: 2
Reputation: 101681
Which exactly is the namespace? Am I correct to call using System; as pre-processor directive?
No, using System;
is just includes the namespace so you can access classes of System
without specifying the namescape.this has nothing to do with preprocessor directives in C#
.
In this case, System
is the namespace but before you include System
namespace with a using directive, you have to add a reference to System.dll
. DLL
files are assemblies which can contain one or more namespaces.
Can I assume namespace My_First_Program works similar to using namespace std in C++ ?
No, namespace My_First_Program
is declaration of a namespace. using namespace std
is similar to using directive in C#
Upvotes: 0
Reputation: 1091
Using is the same as import in java. Your book refered to it as namespace because using imports a namespace.
And yes to my knowledge using namespace std in c++ is the same as in c# using system;
Upvotes: 0
Reputation: 70142
The following statement defines the namespace:
namespace My_First_Program {
...
}
Any code within the braces is within the (wonderfully named) My_First_Program
namespace.
The using
statements 'import' namespaces so that you do not have to use their fully qualified name.
The 'using' statement is not a pre-processor directive. You can find a list of them here.
Upvotes: 2