首页 > 解决方案 > 无法在 C# 中声明任何有符号整数类型

问题描述

我正在使用 Visual Studio 2019 中的 ASP.NET 应用程序。我遇到了一个我以前从未遇到过的 C# 问题。我似乎无法声明 UInt64 以外的任何整数类型。

尝试声明 Int64 或 long 会给我错误,因为它表示我正在尝试将 UInt64(或 ulong)转换为其中一种类型。

具体错误如下(显示的 Azure 租户 ID 是虚拟值,不要惊慌):

线

Int64 tenantInt = 11132432026456784806;

给出以下错误

Error   CS0266  Cannot implicitly convert type 'ulong' to 'long'. An explicit conversion exists (are you missing a cast?)

然后是下面的代码行

long TenantID = -11132432026456784806;

给出以下错误

Error   CS0023  Operator '-' cannot be applied to operand of type 'ulong'

这是相同错误的图像: HERE

我以前在 C# 中工作时从未遇到过这种情况。通常这些声明工作正常。Visual Studio 有问题吗?它不断将数字读取为 UInt64,并尝试使用 (Int64) 或 (long) 进行转换遇到另一个错误,即 UInt64(或 ulong)无法转换为这些类型。

标签: c#asp.netvisual-studiolong-integeruint64

解决方案


您必须使用“ulong”,因为支持 18.446.744.073.709.551.615。

按照参考:https ://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/integral-numeric-types

在此处输入图像描述


推荐阅读