I want to convert a string into an array of bytes. When I try this by use of the Cast-function of LINQ, I get a ‘System.InvalidCastException’. Why?
Here is, what I tried:
var x = "hallo";
var works = x.Select(c=>(byte)c).ToArray();
var doesNotWork = x.Cast<byte>().ToArray();
Enumerable.Castonly performs unboxing and reference conversions. It does not perform other conversions such as the built-in value type ones and user-defined conversions.Before .NET 3.5 SP1 it actually did perform rather more conversions for you. I’m sure the person who performed the code review blogged about it, but I can never remember who it was. (I keep thinking it was Eric Lippert, but it wasn’t.) I’ll look it up.
One thing I’ll say is that this could be better documented.
As it happens, this is the first puzzler in Bill Wagner’s recent video.