Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. how can I convert int[] to object[] ?

how can I convert int[] to object[] ?

Scheduled Pinned Locked Moved C#
questiondata-structureshelptutorial
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    michal kreslik
    wrote on last edited by
    #1

    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

    S 1 Reply Last reply
    0
    • M michal kreslik

      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

      S Offline
      S Offline
      Stefan Troschuetz
      wrote on last edited by
      #2

      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

      www.troschuetz.de

      M 1 Reply Last reply
      0
      • S Stefan Troschuetz

        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

        www.troschuetz.de

        M Offline
        M Offline
        michal kreslik
        wrote on last edited by
        #3

        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

        S 1 Reply Last reply
        0
        • M michal kreslik

          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

          S Offline
          S Offline
          Stefan Troschuetz
          wrote on last edited by
          #4

          michal.kreslik wrote:

          Why string[] is compatible to object[] while int[] is not?

          string and object are reference types (defined as class) whereas int is a value type (defined as struct). 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

          www.troschuetz.de

          M 1 Reply Last reply
          0
          • S Stefan Troschuetz

            michal.kreslik wrote:

            Why string[] is compatible to object[] while int[] is not?

            string and object are reference types (defined as class) whereas int is a value type (defined as struct). 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

            www.troschuetz.de

            M Offline
            M Offline
            michal kreslik
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups