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.
Boxing is the process of converting a value type to the type object or to any interface type implemented by this value type. When the common language runtime (CLR) boxes a value type, it wraps the value inside a System. Object instance and stores it on the managed heap. Unboxing extracts the value type from the object.
Boxing and unboxing is a essential concept in C#'s type system. With Boxing and unboxing one can link between value-types and reference-types by allowing any value of a value-type to be converted to and from type object.
Boxing is an expensive process, since it copies an object from a stack to a heap which requires a number of processor as well as space on the heap. Another disadvantage of using boxing is that the same object appears at two different places in memory which can have contradictory state.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With