I read that all primitives fall under the System
namespace. If I comment out using System
, I would expect there to be a build error in my program. However, it is running successfully. Why is this?
我读到所有原语属于System命名空间。如果我使用System注释掉,我希望我的程序中存在构建错误。但是,它正在成功运行。为什么是这样?
3 个解决方案
#1
23
It's because int
is an alias for System.Int32
, and since the "Int32" is already prefixed with its namespace (ie. "fully qualified"), the syntax is legal without having to specify using System;
at the top of your code.
这是因为int是System.Int32的别名,并且由于“Int32”已经以其命名空间为前缀(即“完全限定”),因此语法是合法的,无需使用System指定;在代码的顶部。
The MSDN snippet below describes this concept-
下面的MSDN片段描述了这个概念 -
Most C# applications begin with a section of using directives. This section lists the namespaces that the application will be using frequently, and saves the programmer from specifying a fully qualified name every time that a method that is contained within is used. For example, by including the line:
大多数C#应用程序都以using指令的一部分开头。本节列出了应用程序将经常使用的命名空间,并且每次使用其中包含的方法时,都会使程序员不会指定完全限定的名称。例如,通过包含以下行:
using System;
At the start of a program, the programmer can use the code:
在程序开始时,程序员可以使用以下代码:
Console.WriteLine("Hello, World!");
Instead of:
代替:
System.Console.WriteLine("Hello, World!");
System.Int32
(aka "int") would be the latter. Here is an example of this in code -
System.Int32(又名“int”)将是后者。以下是代码中的示例 -
//using System;
namespace Ns
{
public class Program
{
static void Main(string[] args)
{
System.Int32 i = 2; //OK, since we explicitly specify the System namespace
int j = 2; //alias for System.Int32, so this is OK too
Int32 k = 2; //Error, because we commented out "using System"
}
}
}
Since line 11 is not fully qualified / aliasing a fully qualified type, using System;
would need to be uncommented for the error to go away.
由于第11行不是完全限定/别名的完全限定类型,因此使用System;需要取消注释才能使错误消失。
Additional references-
其他参考 -
-
C#, int or Int32? Should I care?
C#,int还是Int32?我应该关心吗?
-
Built-In Types Table (C# Reference) (Lists all the built-in types, and their .NET framework equivalents)
内置类型表(C#参考)(列出所有内置类型及其.NET框架等价物)
#2
8
As was mention before int
is an alias of System.Int32
type. The alias of primitive types are implicitly known by the C# language. Here is the list:
如前所述,int是System.Int32类型的别名。 C#语言隐含地知道原始类型的别名。这是清单:
object: System.Object
string: System.String
bool: System.Boolean
byte: System.Byte
sbyte: System.SByte
short: System.Int16
ushort: System.UInt16
int: System.Int32
uint: System.UInt32
long: System.Int64
ulong: System.UInt64
float: System.Single
double: System.Double
decimal: System.Decimal
char: System.Char
So, for these aliases, also known as simple types, you don't need to specify any namespace.
因此,对于这些别名(也称为简单类型),您不需要指定任何名称空间。
#3
4
When you use int, you are basically putting in System.Int32. Since this is the fully qualified type name, you don't actually need using System;
当你使用int时,你基本上放入System.Int32。由于这是完全限定的类型名称,因此您实际上不需要使用System;
Your program would work if you did
如果你这样做,你的计划将会奏效
System.Int32 num = 0;
even without the using
即使没有使用
#1
23
It's because int
is an alias for System.Int32
, and since the "Int32" is already prefixed with its namespace (ie. "fully qualified"), the syntax is legal without having to specify using System;
at the top of your code.
这是因为int是System.Int32的别名,并且由于“Int32”已经以其命名空间为前缀(即“完全限定”),因此语法是合法的,无需使用System指定;在代码的顶部。
The MSDN snippet below describes this concept-
下面的MSDN片段描述了这个概念 -
Most C# applications begin with a section of using directives. This section lists the namespaces that the application will be using frequently, and saves the programmer from specifying a fully qualified name every time that a method that is contained within is used. For example, by including the line:
大多数C#应用程序都以using指令的一部分开头。本节列出了应用程序将经常使用的命名空间,并且每次使用其中包含的方法时,都会使程序员不会指定完全限定的名称。例如,通过包含以下行:
using System;
At the start of a program, the programmer can use the code:
在程序开始时,程序员可以使用以下代码:
Console.WriteLine("Hello, World!");
Instead of:
代替:
System.Console.WriteLine("Hello, World!");
System.Int32
(aka "int") would be the latter. Here is an example of this in code -
System.Int32(又名“int”)将是后者。以下是代码中的示例 -
//using System;
namespace Ns
{
public class Program
{
static void Main(string[] args)
{
System.Int32 i = 2; //OK, since we explicitly specify the System namespace
int j = 2; //alias for System.Int32, so this is OK too
Int32 k = 2; //Error, because we commented out "using System"
}
}
}
Since line 11 is not fully qualified / aliasing a fully qualified type, using System;
would need to be uncommented for the error to go away.
由于第11行不是完全限定/别名的完全限定类型,因此使用System;需要取消注释才能使错误消失。
Additional references-
其他参考 -
-
C#, int or Int32? Should I care?
C#,int还是Int32?我应该关心吗?
-
Built-In Types Table (C# Reference) (Lists all the built-in types, and their .NET framework equivalents)
内置类型表(C#参考)(列出所有内置类型及其.NET框架等价物)
#2
8
As was mention before int
is an alias of System.Int32
type. The alias of primitive types are implicitly known by the C# language. Here is the list:
如前所述,int是System.Int32类型的别名。 C#语言隐含地知道原始类型的别名。这是清单:
object: System.Object
string: System.String
bool: System.Boolean
byte: System.Byte
sbyte: System.SByte
short: System.Int16
ushort: System.UInt16
int: System.Int32
uint: System.UInt32
long: System.Int64
ulong: System.UInt64
float: System.Single
double: System.Double
decimal: System.Decimal
char: System.Char
So, for these aliases, also known as simple types, you don't need to specify any namespace.
因此,对于这些别名(也称为简单类型),您不需要指定任何名称空间。
#3
4
When you use int, you are basically putting in System.Int32. Since this is the fully qualified type name, you don't actually need using System;
当你使用int时,你基本上放入System.Int32。由于这是完全限定的类型名称,因此您实际上不需要使用System;
Your program would work if you did
如果你这样做,你的计划将会奏效
System.Int32 num = 0;
even without the using
即使没有使用