Krutika Patel
Krutika Patel

Reputation: 430

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(); this line is throwing an error like "Cannot implicitly convert 'byte[]' to 'byte?[]'"

Please help me on this.

Upvotes: 0

Views: 1201

Answers (2)

Ygalbel
Ygalbel

Reputation: 5519

Another answer with Linq:

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

Upvotes: 2

Cade Weiskopf
Cade Weiskopf

Reputation: 91

Maybe you can do something like this:

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

Upvotes: 4

Related Questions