相关疑难解决方法(0)

byte + byte = int ...为什么?

看看这个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=int
  • long+ long=long
  • float+ float=float
  • double+ double=double

那么为什么不呢:

  • byte+ byte=byte
  • short+ short= short

一点背景:我正在对"小数字"(即<8)执行一长串计算,并将中间结果存储在一个大数组中.使用字节数组(而不是int数组)更快(因为缓存命中).但是通过代码传播的大量字节转换使得它更加难以理解.

c# type-conversion

356
推荐指数
8
解决办法
6万
查看次数

条件运算符不能隐式转换?

我对这个小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

c# types conditional-operator implicit-cast

61
推荐指数
3
解决办法
4797
查看次数

为什么ushort + ushort等于int?

以前我今天尝试添加两个ushorts,我注意到我必须将结果反馈给ushort.我认为它可能会成为一个uint(以防止可能的意外溢出?),但令我惊讶的是它是一个int(System.Int32).

是否有一些聪明的理由或者是因为i​​nt被视为'基本'整数类型?

例:

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值进行操作?

c# types integer-arithmetic

28
推荐指数
3
解决办法
1万
查看次数

为什么C#中的字节减法需要强制转换?

我必须使用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是整数类型...那么我为什么需要演员?

.net c# byte integer casting

15
推荐指数
3
解决办法
9676
查看次数

短+短!=短?

版本: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# short visual-studio-2013

6
推荐指数
1
解决办法
453
查看次数

C#按位OR有时需要使用byte*进行转换*

我在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)

谢谢.

c# bitwise-operators

5
推荐指数
3
解决办法
1万
查看次数

C#中的OR-ing字节给出了int

我有这个代码.

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)

c# logic byte types bit-manipulation

4
推荐指数
2
解决办法
2531
查看次数

如何清除字节中的位?

我需要设置清除字节数组的一些字节.

设置工作良好,但清除不编译(我猜因为否定有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)

如何以一种优选的 …

c# byte bit-manipulation

2
推荐指数
1
解决办法
7387
查看次数

将VB转换为C#

我有一个用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)

c# vb.net code-translation

1
推荐指数
1
解决办法
312
查看次数