Chris Nickson
Chris Nickson

Reputation: 105

C# int32 literal can only be stored in long data type

Im preparing for a very tricky c# exam and this question popped up while doing so. I have the following code:

 uint zzz = -12u;

-12u is recognized as System.Uint32 literal but it can only be stored in variable of type long. Why is that ?

Upvotes: 5

Views: 435

Answers (2)

Richard Dalton
Richard Dalton

Reputation: 35793

What it is doing is taking the unsigned int 12 and trying to convert it into a negative value (the -) which requires a conversion to a type that can handle negative numbers as an unsigned int cannot.

Because it is an unsigned int it has possible values outside the range of int, so conversion to a long is required.

Upvotes: 12

Ravindra Bagale
Ravindra Bagale

Reputation: 17665

because u is for unsigned int
for handling -ve sign it converting it into Long data type
-12u is a signed int data type & for storing it in unsigned type it uses long data type

Upvotes: 0

Related Questions