为什么需要投在C#字节减法? [英] Why is a cast required for byte subtraction in C#?

查看:237
本文介绍了为什么需要投在C#字节减法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要在VS2008 .NET 3.5使用的WinForms以下code:

I have to following code in VS2008 .net 3.5 using WinForms:

byte percent = 70;
byte zero = 0;

Bitmap copy = (Bitmap)image1.Clone();
...

Color oColor = copy.GetPixel(x, y);
byte oR = (byte)(oColor.R - percent < zero ? zero : oColor.R - percent);

当我离开 (字节) 关闭的code中的最后一行,我得到一个编译器错误说它无法隐式转换类型' INT '到'字节'。如果一切类型的字节字节是一个整数类型......那么为什么我需要有投?

When I leave the "(byte)" off the last line of code, I get a compiler error saying it "Cannot implicitly convert type 'int' to 'byte'." If everything is of type byte and byte is an integer type... then why do I need to have the cast?

推荐答案

由于减法强迫达一个整数。我记得,字节是一个无符号类型在C#中,所以减法可以带你出去字节的域。

Because subtraction is coercing up to an integer. As I recall, byte is an unsigned type in C#, so subtraction can take you out of the domain of bytes.

这篇关于为什么需要投在C#字节减法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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