看看这个C#代码:
byte x = 1;
byte y = 2;
byte z = x + y; // ERROR: Cannot implicitly convert type 'int' to 'byte'
Run Code Online (Sandbox Code Playgroud)
对byte(或short)类型执行的任何数学运算的结果都隐式地转换回整数.解决方案是将结果显式地转换回一个字节:
byte z = (byte)(x + y); // this works
Run Code Online (Sandbox Code Playgroud)
我想知道的是为什么?它是建筑吗?哲学?
我们有:
int+ int=intlong+ long=longfloat+ float=floatdouble+ double=double那么为什么不呢:
byte+ byte=byteshort+ short= short?一点背景:我正在对"小数字"(即<8)执行一长串计算,并将中间结果存储在一个大数组中.使用字节数组(而不是int数组)更快(因为缓存命中).但是通过代码传播的大量字节转换使得它更加难以理解.
我对这个小C#quirk感到有点难过:
给定变量:
Boolean aBoolValue;
Byte aByteValue;
Run Code Online (Sandbox Code Playgroud)
以下编译:
if (aBoolValue)
aByteValue = 1;
else
aByteValue = 0;
Run Code Online (Sandbox Code Playgroud)
但这不会:
aByteValue = aBoolValue ? 1 : 0;
Run Code Online (Sandbox Code Playgroud)
错误说:"不能隐式地将类型'int'转换为'byte'."
当然,这个怪物会编译:
aByteValue = aBoolValue ? (byte)1 : (byte)0;
Run Code Online (Sandbox Code Playgroud)
这里发生了什么?
编辑:
使用VS2008,C#3.5
以前我今天尝试添加两个ushorts,我注意到我必须将结果反馈给ushort.我认为它可能会成为一个uint(以防止可能的意外溢出?),但令我惊讶的是它是一个int(System.Int32).
是否有一些聪明的理由或者是因为int被视为'基本'整数类型?
例:
ushort a = 1;
ushort b = 2;
ushort c = a + b; // <- "Cannot implicitly convert type 'int' to 'ushort'. An explicit conversion exists (are you missing a cast?)"
uint d = a + b; // <- "Cannot implicitly convert type 'int' to 'uint'. An explicit conversion exists (are you missing a cast?)"
int e = a + b; // <- Works!
Run Code Online (Sandbox Code Playgroud)
编辑:就像GregS的回答所说,C#规范说两个操作数(在这个例子中是'a'和'b')应该转换为int.我对为什么这是规范的一部分的根本原因感兴趣:为什么C#规范不允许直接对ushort值进行操作?
我必须使用WinForms在VS2008 .net 3.5中使用以下代码:
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);
Run Code Online (Sandbox Code Playgroud)
当我离开(byte)最后一行代码的" "时,我收到编译器错误,说它"无法隐式转换类型int"到' byte'.如果一切都是类型byte并且byte是整数类型...那么我为什么需要演员?
版本:Visual Studio Professional 2013 Update 4
构建参数:首选32位为真
我不明白以下C#代码中的错误:
short iCount = 20;
short iValue = iCount + (short)1;
Run Code Online (Sandbox Code Playgroud)
将short添加到转换为short的int会导致以下错误:
无法将类型'int'隐式转换为'short'.存在显式转换(您是否错过了演员?)
上面的错误,在下面的例子中也可以看到,在这里完全有效:
short iCount = 20;
short iValue = iCount + 1;
Run Code Online (Sandbox Code Playgroud)
以下解决方法将删除错误:
short iCount = 20;
short iValue = (short)(iCount + 1);
Run Code Online (Sandbox Code Playgroud)
因此,"short + Int32 constant"形式的加法显然有效,结果是Int32,需要将其缩短.
有没有解释为什么第一个代码示例失败或者这是编译器错误?(在4次更新后我几乎无法相信)
我在C#编译器中发现了奇怪的情况.为什么需要下面的演员?
using System;
class Program
{
private const byte BIT_ZERO_SET = 1;
private const byte BIT_ONE_SET = 2;
private const byte BIT_TWO_SET = 4;
static void Main(string[] args)
{
byte b = BIT_ZERO_SET | BIT_ONE_SET;
Console.WriteLine(b);
//Does not compile, says needs to cast to int.
//b = b | BIT_TWO_SET;
//Compiles...ugly
b = (byte)(b | BIT_TWO_SET);
Console.WriteLine(b);
Console.WriteLine("Press enter.");
Console.ReadLine();
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢.
我有这个代码.
byte dup = 0;
Encoding.ASCII.GetString(new byte[] { (0x80 | dup) });
Run Code Online (Sandbox Code Playgroud)
当我尝试编译时,我得到:
无法将类型'int'隐式转换为'byte'.存在显式转换(您是否错过了演员?)
为什么会这样?不应该| 两个字节给出一个字节?以下两项工作,确保每个项目都是一个字节.
Encoding.ASCII.GetString(new byte[] { (dup) });
Encoding.ASCII.GetString(new byte[] { (0x80) });
Run Code Online (Sandbox Code Playgroud) 我需要设置和清除字节数组的一些字节.
设置工作良好,但清除不编译(我猜因为否定有int作为结果,它是负面的,然后...).
public const byte BIT_1 = 0x01;
public const byte BIT_2 = 0x02;
byte[] bytes = new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
// set 1st bit
for (int i = 0; i < bytes.Length; i++)
bytes[i] |= BIT_1;
// clear 2nd bit (set it to 0)
for (int i = 0; i < bytes.Length; i++)
bytes[i] &= (~BIT_2);
Run Code Online (Sandbox Code Playgroud)
如何以一种优选的 …
我有一个用VB编写的加密类,我试图将其转换为C#.在VB代码中,有一段代码:
' Allocate byte array to hold our salt.
Dim salt() As Byte = New Byte(saltLen - 1) {}
' Populate salt with cryptographically strong bytes.
Dim rng As RNGCryptoServiceProvider = New RNGCryptoServiceProvider()
rng.GetNonZeroBytes(salt)
' Split salt length (always one byte) into four two-bit pieces and
' store these pieces in the first four bytes of the salt array.
salt(0) = ((salt(0) And &HFC) Or (saltLen And &H3))
salt(1) = ((salt(1) And &HF3) Or (saltLen And &HC))
salt(2) = ((salt(2) …Run Code Online (Sandbox Code Playgroud)