0

I'm working on an ASP.NET application in Visual Studio 2019. I'm running into a problem I've never encountered with C# before. I seemingly cannot declare any integer types other than UInt64.

Trying to declare an Int64 or a long gives me errors, because it says I'm trying to convert a UInt64 (or ulong) into one of those types.

The particular errors are as follows (the Azure tenant ids shown are dummy values, don't panic):

The line

Int64 tenantInt = 11132432026456784806;

gives the following error

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

and then the following line of code

long TenantID = -11132432026456784806;

gives the following error

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

Here is an image of the very same errors: HERE

I've never encountered this when working in C# before. Normally these declarations work fine. Is there something wrong with Visual Studio? It keeps reading the numbers as UInt64, and trying to cast them using (Int64) or (long) is met with another error, that a UInt64 (or ulong) cannot be cast to those types.

  • 9
    Because 11132432026456784806 is bigger than long.MaxValue – Kalten Dec 14 '20 at 20:34
  • Kalten is right. As a workaround, you could try to use `decimal` instead. – Xaver Dec 14 '20 at 20:35
  • `ulong` is the smallest type for that value... **9**A7E57F227237FA6... Someone will reply with link to spec that explain that. – Alexei Levenkov Dec 14 '20 at 20:36
  • @Alexei: Well, `11132432026456784806` can be stored in an `ulong`. But you have to write `UInt64` instead of `Int64` to achieve this. -11132432026456784806, however, cannot be stored in a `long`. – Xaver Dec 14 '20 at 20:39
  • For `ulong`, the minimum value is 0 and the maximum value is (2^64)-1. For `long`, the minimum value is -(2^63) and the maximum value is (2^63)-1. – Xaver Dec 14 '20 at 20:42
  • 2
    Speaking of Azure tenant IDs -- those are not integers at all, but GUIDs. That's even more bits than you're trying to stuff into a 64 (or 63) bit integer here. – Jeroen Mostert Dec 14 '20 at 20:45
  • @Kalten You are correct. That was it. Essentially, the guy I got this API from purposefully made the number too big so the line would throw an error so I would see it and realize I need to update the Tenant ID. – BradleyKern Dec 14 '20 at 22:27

2 Answers2

1

You must use the "ulong" because support 18.446.744.073.709.551.615.

Follow the reference: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/integral-numeric-types

enter image description here

0

Kalten is correct. The numbers I'm trying to assign are too big for Int64 and long. Essentially, the person I got this API from purposefully made the number too big so the line would throw an error so I would see the line and realize I need to replace it with my own ID.