Reputation: 2703
I want to find the best approach for converting bool[]
into object[]
in C# .NET 4.0.
Now I have this variables:
object[] objectArray = new object [] { true, false, true };
string[] stringArray = new string[] { "true", "false", "true" };
bool[] boolArray = new bool[] { true, false, true };
All are created fine. For 'clear types', suc as bool
and object
, boxing works fine (object o = true;
). But in this case I can do conversion only from a string array to an object array, but not from a boolean array:
objectArray = stringArray; // OK
objectArray = boolArray; // WRONG Cannot implicitly convert bool[] to object[]
Also, in some methods I am sending a list of object arrays. As in previous case, I can do this (conversion) for string, but not for a boolean array:
List<object[]> testList;
testList = new List<object[]>() { objectArray }; // OK
testList = new List<object[]>() { stringArray }; // OK
testList = new List<object[]>() { boolArray }; // WRONG - I can not add bool[] into object[]
From some methods, I have a boolean array with many items inside ... and the last method, after all calculations, returns an object array as a result (sometimes it must return other types and I don't want to split it into multiple methods).
Whereas, I can not use return_object_array = boolean_array
. What is the best method for doing this? Is looping over all values in a boolean array and storing it into an object array the fastest way?
What is the best / fastest / most correct approach to do this?
Note: This method is writen under .NET 4.0, but if you know a better solution for .NET 4.5 I'd like to know it.
Upvotes: 40
Views: 11323
Reputation: 1501043
It sounds like you just need to box each value, right? That's as simple as:
object[] objectArray = boolArray.Select(b => (object) b).ToArray();
Or even:
object[] objectArray = boolArray.Cast<object>().ToArray();
(As Cast
will perform boxing/unboxing operations.)
Or slightly more efficiently in terms of knowing the correct size to start with:
object[] objectArray = Array.ConvertAll(boolArray, b => (object) b);
Alternatively, change your APIs to not require an object[]
to start with. Consider using generic methods/types instead.
EDIT: To avoid the boxing each time, you can easily write your own extension class similar to the framework one nmclean showed:
public static class BooleanBoxExtensions
{
private static readonly object BoxedTrue = true;
private static readonly object BoxedFalse = false;
public static object BoxCheaply(this bool value)
{
return value ? BoxedTrue : BoxedFalse;
}
}
Then:
object[] objectArray = Array.ConvertAll(boolArray, b => b.BoxCheaply());
Or:
object[] objectArray = boolArray.Select(BooleanBoxExtensions.BoxCheaply)
.ToArray();
Upvotes: 79
Reputation: 15364
bool[] boolArray = { true, false, true };
var objArray = Array.ConvertAll(boolArray, x => (object)x);
Upvotes: 7
Reputation: 26301
Cast<T>
method performs boxing.
var castedValues = boolArray.Cast<object>();
objectArray = castedValues.ToArray();
Upvotes: 11