how can I convert int[] to object[] ?
-
Hello, how can I convert int[] array to object[] array? I have written a method that compares two arrays and takes (object[] FirstArray, object[] SecondArray) as input arguments. If I pass string[] arrays as arguments, it works fine, but if I pass int[] array, compiler reports an error "Cannot convert type 'int[]' to 'object[]'". Example code:
string\[\] StringArray; int\[\] IntArray; object\[\] ObjectArray; ObjectArray = (object\[\])StringArray; // OK ObjectArray = (object\[\])IntArray; // Cannot convert type 'int\[\]' to 'object\[\]'
I thought that by converting from int[] to object[] I am doing a standard boxing operation. How can I resolve this? Thanks much, Michal
-
Hello, how can I convert int[] array to object[] array? I have written a method that compares two arrays and takes (object[] FirstArray, object[] SecondArray) as input arguments. If I pass string[] arrays as arguments, it works fine, but if I pass int[] array, compiler reports an error "Cannot convert type 'int[]' to 'object[]'". Example code:
string\[\] StringArray; int\[\] IntArray; object\[\] ObjectArray; ObjectArray = (object\[\])StringArray; // OK ObjectArray = (object\[\])IntArray; // Cannot convert type 'int\[\]' to 'object\[\]'
I thought that by converting from int[] to object[] I am doing a standard boxing operation. How can I resolve this? Thanks much, Michal
object[] ObjectArray = new object[IntArray.Length];
IntArray.CopyTo(ObjectArray, 0);
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook
-
object[] ObjectArray = new object[IntArray.Length];
IntArray.CopyTo(ObjectArray, 0);
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook
Thanks, but this doesn't solve the problem of a method that takes object[] as a universal argument. Why string[] is compatible to object[] while int[] is not? It's not too much elegant to always convert int[] to object[] by using CopyTo before passing the parameters to the method. There must be some more conceptual way. Thanks, Michal
-
Thanks, but this doesn't solve the problem of a method that takes object[] as a universal argument. Why string[] is compatible to object[] while int[] is not? It's not too much elegant to always convert int[] to object[] by using CopyTo before passing the parameters to the method. There must be some more conceptual way. Thanks, Michal
michal.kreslik wrote:
Why string[] is compatible to object[] while int[] is not?
string
andobject
are reference types (defined asclass
) whereasint
is a value type (defined asstruct
). Obviously it's not possible to simply cast an array of value types to an array of reference types. I guess that's due to the necessary boxing of array items. I agree that the use of CopyTo isn't the most elegant solution but I think there's no other way when dealing with value type arrays.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook
-
michal.kreslik wrote:
Why string[] is compatible to object[] while int[] is not?
string
andobject
are reference types (defined asclass
) whereasint
is a value type (defined asstruct
). Obviously it's not possible to simply cast an array of value types to an array of reference types. I guess that's due to the necessary boxing of array items. I agree that the use of CopyTo isn't the most elegant solution but I think there's no other way when dealing with value type arrays.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook
Actually, after some fiddling, I've found a way how to do it. You don't need to "cast" the type if you use a generic method: class Program { static void MyMethodWithUnknownInputType(T[] Argument) { Console.WriteLine("Current input type is: {0}", Argument.GetType().ToString()); Console.WriteLine("Array contents:"); foreach (T Item in Argument) { Console.Write("{0} ", Item.ToString()); } Console.WriteLine("\n"); } static void Main() { string[] MyStringArray = { "String1", "String2", "String3", "String4", "String5" }; int[] MyIntArray = { 2, 4, 8, 16, 32 }; MyMethodWithUnknownInputType(MyStringArray); MyMethodWithUnknownInputType(MyIntArray); } } The output of this program: Current input type is: System.String[] Array contents: String1 String2 String3 String4 String5 Current input type is: System.Int32[] Array contents: 2 4 8 16 32 Michal -- modified at 18:36 Wednesday 13th September, 2006