C# 中 int 和 Int32 有什么区别?

csharpserver side programmingprogramming更新于 2025/10/2 12:52:17

Int32 是 .NET 框架提供的类型,而 int 是 C# 语言中 Int32 的别名。

  • Int32 x = 5;

  • int x = 5;

因此,在使用中,上述两个语句都将保存一个 32 位整数。它们编译为相同的代码,因此在执行时没有任何区别。

唯一的细微区别是 Int32 只能用于 System 命名空间。在验证上述值的类型时,我们可以使用 Int32 或 int。

typeof(int) == typeof(Int32) == typeof(System.Int32)

示例

以下示例展示了如何使用 System.Int32 声明整数。

using System;
namespace DemoApplication{
   class Program{
      static void Main(string[] args){
         Int32 x = 5;
         Console.WriteLine(x); //Output: 5
      }
   }
}

输出

5

示例

下面的例子展示了如何使用 int 关键字声明一个整数。

using System;
namespace DemoApplication{
   class Program{
      static void Main(string[] args){
         int x = 5;
         Console.WriteLine(x); //Output: 5
      }
   }
}

输出

5

相关文章