I tried the following
// no using statements here, not even using System;
class Program
{
static void Main(string[] args)
{
string[] arr = new string[3] { "A" , "B", "C" };
}
}
My question is simple, even without any using statements, how come compiler is able t开发者_如何学Co compile this, more specifically, shouldn't there be a compile time error because I am trying to build an array and System.Array is not accessible?
Thanks.
You seem to have a misunderstanding about what using
means. A using
directive is for determining what a simple name means in your program at compile time. When you say using Foo;
that means "if the text of this program uses the simple name Bar
, and it doesn't seem like anything named Bar
is in scope where the name is used then try checking to see if there is a type called Bar
inside namespace Foo
".
Your program does not contain any simple names! (string
is not a simple name; it is a keyword of the language.) Therefore there is no need to have any using
directive to resolve the non-existing simple names.
using
does not at all mean "this program uses features that require types found in this assembly", which is what you seem to think it means. using
directives are only about (1) simple name resolution, and (2) extension method name resolution.
Arrays are part of the language. You don't need to reference a namespace to use them.
string[]
will translate to System.Array
while compiling. Since its already fully qualified, it doesn't need any using
statements.
You're not referring by name to any namespaced type but yours (the Program
class).
string
and []
are aliases that are indeed equivalent to System.String
and System.Array
respectively, but using them does not require pulling their counterparts' namespaces into scope.
You aren't directly referencing the Array
class or its members, so the compiler won't complain that you're trying to reference an unused namespace or type. You're just using notation that's part of the C# language itself to make arrays.
This is because you are using core things, you don't even need System
for this to compile. The System
namespace has extended functionality, some of the classes included in the System Namespace is:
- Action
- Func
- Console
- And many more..
As you are not trying to write something to the command prompt, creating actions or even asking for date using DateTime
, you don't need to import anything.
using statements do not include anything or provide any ekstra code. It only makes it simplier to code, because you don't have to write the whole path every time.
string
automatically converts to System.String.
You can write this:
class Foo {
string Bar() {
return System.IO.File.Exists("path").ToString();
}
}
or just
using System.IO;
class Foo {
string Bar() {
return File.Exists("path").ToString();
}
}
both compiles to the same program.
Namespaces are not necessary, you can organize with namespaces your code but you don't have to. And common keywords are automatically referenced to the proper namespace.
For better understanding read this msdna document http://msdn.microsoft.com/en-us/library/ms973231.aspx
精彩评论