Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
374 views
in Technique[技术] by (71.8m points)

arrays - Correct way to boxing bool[] into object[] in C#

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.

question from:https://stackoverflow.com/questions/21986249/correct-way-to-boxing-bool-into-object-in-c-sharp

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...