开发者

How to do this without unchecked?

a few months ago I wrote this code because it was the only way I could think to do it(while learning C#), well. How would you do it? Is unchecked the proper way of doing this?

unchecked //FromArgb takes a 32 bit value, though says it's signed. Which colors shouldn't be.
{
  _EditControl.BackColor = System.Drawing.Color.FromArgb((int)0开发者_如何学GoxFFCCCCCC);
}


It takes a signed int b/c this dates back to the time when VB.NET didn't have unsigned values. So in order to maintain compatibility between C# and VB.NET, all the BCL libraries utilize signed values, even if it does not make logical sense.


You could break down the components of the int and use the FromArgb() overload that takes them separately:

System.Drawing.Color.FromArgb( 0xFF, 0xCC, 0xCC, 0xCC);


Extension methods can hide this:

public static Color ToColor(this uint argb)
{
    return Color.FromArgb(unchecked((int)argb));
}

public static Color ToColor(this int argb)
{
    return Color.FromArgb(argb);
}

Usage:

0xff112233.ToColor(); 
0x7f112233.ToColor();

Seems like they're should be another notation (like 0v12345678) or some other way to work around this issue.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜