【问题标题】:Cannot implicitly convert type 'byte[]' to 'byte?[]' in C#无法在 C# 中将类型 'byte[]' 隐式转换为 'byte?[]'
【发布时间】:2021-12-02 04:01:30
【问题描述】:

我给定的代码有类型转换错误:

                byte?[] AibAttachment = null; 
                MemoryStream target = new MemoryStream();
                file.InputStream.CopyTo(target);
                AibAttachment = target.ToArray();
           

在上面的代码中 AibAttachment = target.ToArray();这一行抛出了一个错误,比如“不能将'byte []'隐式转换为'byte?[]'”

请帮帮我。

【问题讨论】:

  • Array<byte>Array<Nullable<byte>> 不同,一个可以保存空值,另一个不能。您可能只想要byte[] AibAttachment = null;。更好的是,只需var AibAttachment = target.ToArray();
  • @CamiloTerevinto,好的,但是我必须将此字节数组传递给某个第三方 API,并且 AibAttachment 变量是该 API 请求的一部分,它是字节类型的?[]

标签: c# arrays .net


【解决方案1】:

也许你可以这样做:

AibAttachment = Array.ConvertAll(target.ToArray(), i => (byte?)i);

【讨论】:

  • 感谢它拯救了我的一天!!!
【解决方案2】:

Linq 的另一个答案:

byte[] original = null; // something 
byte?[] AibAttachment =  original.Select(a => (byte?) a).ToArray();

【讨论】:

    猜你喜欢
    • 2018-09-05
    • 1970-01-01
    • 2020-07-03
    • 2019-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-01
    • 1970-01-01
    相关资源
    最近更新 更多