简体   繁体   English

无法在 C# 中将类型“byte[]”隐式转换为“byte?[]”

[英]Cannot implicitly convert type 'byte[]' to 'byte?[]' in C#

My given code has an error of type conversion:我给定的代码有类型转换错误:

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

In above code AibAttachment = target.ToArray();在上面的代码中 AibAttachment = target.ToArray(); this line is throwing an error like "Cannot implicitly convert 'byte[]' to 'byte?[]'"这一行抛出了一个错误,比如“不能隐式地将‘字节[]’转换为‘字节?[]’”

Please help me on this.请帮我解决这个问题。

也许你可以做这样的事情:

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

Another answer with Linq: Linq的另一个答案:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM