I had a real scenario 5 minutes ago where I needed to turn a Guid[]
into an object[]
.
The dead simple and quick way out of this is to type:
var dataset = inputArray.Select(item => (object)item).ToArray();
Readable and everything, but I'm not sure it is very effective (could of course be the case that the compiler optimizes it a bit).
What would you suggest is best to go from type to type (assuming it is castable between, skipping integer parsing and the like)?
EDIT: Cast<T>
extension method is of course also usable.
var listOfGuids = new Guid[]{Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid()};
var listOfStrings = new string[]{"foo", "bar"};
var objectListGuidsLinq = listOfGuids.Cast<object>().ToArray();
var objectListStringsLinq = listOfStrings.Cast<object>().ToArray();
var objectListStringsDirect = (object[]) listOfStrings;