C# - 恒定值'4294901760'不能转换为'廉政' [英] C# - Constant value '4294901760' cannot be converted to a 'int'

查看:267
本文介绍了C# - 恒定值'4294901760'不能转换为'廉政'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候,

我简直不敢相信我在问这样一个基本的问题,但它没有任何意义所以在这里,它是:。)

I can't believe I'm asking such a basic question, but it doesn't make sense so here it is :).

在C#在Windows Phone 7 / .NET,我想在一个类中定义常量如下:

In C# on Windows Phone 7 / .net, I'm trying to define a constant in a class as follows:

// error CS0266: Cannot implicitly convert type 'uint' to 'int'. 
// An explicit conversion exists (are you missing a cast?)
public const int RED = 0xffff0000;


$ B $周围铸像因此B

如果我把一个(INT),我得到另一个错误:

If I put an (int) cast around it like so, I get another error:

// error CS0221: Constant value '4294901760' cannot be converted to a 'int' 
// (use 'unchecked' syntax to override)        
public const int RED = (int)0xffff0000;



但我知道,我的int是32位的,所以有一系列的2,147,483,648 2,147,483,647,看到 http://msdn.microsoft.com/en -us /库/ 5kzh1b5w(v = VS.80)的.aspx

那么怎么办?

在此先感谢!

推荐答案

正如你注意,的Int32 的范围是-2,147,483,648到2,147,483,647,使范围内的任何数字可以举行,但该范围内的只有数字可以举行。 4294901760大于2,147,483,647越大,因此不适合在的Int32

As you note, the range of Int32 is -2,147,483,648 to 2,147,483,647, so any number within that range can be held, but ONLY numbers within that range can be held. 4,294,901,760 is greater than 2,147,483,647, so doesn't fit in an Int32.

如何对待这个要看你想达到什么。如果你只是想要一个的Int32 的位模式 FFFF0000 ,然后根据提示使用选中

What to do about this depends on what you want to achieve. If you just want an Int32 with the bit pattern ffff0000, then as suggested use unchecked :

int y = unchecked((int)0xffff0000);



现在的值为-65536,这是解释为一个有符号整数位模式。

y now has the value -65536, which is that bit pattern interpreted as a signed integer.

不过!如果你真的想要的值4294901760,你应该使用适合于它的数据类型 - 所以 UInt32的

However! If you actually want the value 4,294,901,760 you should use a datatype appropriate to it - so UInt32.

这篇关于C# - 恒定值'4294901760'不能转换为'廉政'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆