locked
Casting Arrays? RRS feed

  • Question

  • Hi,

    I have a method which returns an object array:

    object[] sortArray(object[] input, int[] order);

    And I want to be able to cast the array that it returns, for example:

    string[] array = (string[])sortArray(input, order);

    While this compiles, at run time I get a casting error (InvalidCastException, Specified cast is not valid). How should I be casting the array, or can I cast it at all?

    Thanks.

    Monday, July 10, 2006 11:03 AM

Answers

  • here Is a much better way:

    string[] array = Array.ConvertAll<object, string>(input,

        delegate(object obj)

        {

            return (string)obj;

        });

     

    And here is one that doesn’t use generics:

     

    string[] array = new string[input.Length];

    for (int index = 0; index < input.Length; index++)

    {

        array[index] = (string)input[index];

    }

    Tuesday, July 11, 2006 8:43 AM
  • You should probably iterate through the objects in the list and cast each item separately. In untested code:

    string[] stringArray;

    stringArray = new string[objectsArray.Count]();
    foreach (object obj in objectsArray)
    {
      stringArray[objectsArray.IndexOf(obj)] = obj.ToString();
    }
    Monday, July 10, 2006 11:17 AM

All replies

  • You should probably iterate through the objects in the list and cast each item separately. In untested code:

    string[] stringArray;

    stringArray = new string[objectsArray.Count]();
    foreach (object obj in objectsArray)
    {
      stringArray[objectsArray.IndexOf(obj)] = obj.ToString();
    }
    Monday, July 10, 2006 11:17 AM
  • here Is a much better way:

    string[] array = Array.ConvertAll<object, string>(input,

        delegate(object obj)

        {

            return (string)obj;

        });

     

    And here is one that doesn’t use generics:

     

    string[] array = new string[input.Length];

    for (int index = 0; index < input.Length; index++)

    {

        array[index] = (string)input[index];

    }

    Tuesday, July 11, 2006 8:43 AM
  • Using Linq this can be done in one line. Given some array (myArray), one can cast every element to a string and produce a new string array.

    string[] stringVersion = myArray.Select(item => (string)item).ToArray();

     

     

     

    Sunday, January 2, 2011 10:44 PM
  • Using Linq this can be done in one line. Given some array (myArray), one can cast every element to a string and produce a new string array.

    string[] stringVersion = myArray.Select(item => (string)item).ToArray();

     

    You do realize that you just answered a post thats 4 1/2 years old and then you marked your own post as an answer lol.

     

     

     

     

     

    Monday, January 3, 2011 12:47 AM
  • The Cast method is better for this job.

    string[] stringVersion = myArray.Cast<string>().ToArray();
    
    
    
    Monday, January 3, 2011 5:10 PM
  • Hello,

    quite a long time I'm looking for a way how to cast an input array to and output array of another type in a fast way. The method must be flexible, in other words I need it as a generic method for ValueTypes (primitive type as int, float,...) only. Currently I have an autogenerated class that detects the type at runtime and does a lot of ifs and elses to find the current input and output type and then does a cast on it to make it possible to get a pointer on it.

    Does someone has a solution how I can make this simpler?

     

    Thanks

    Martin

    Tuesday, July 12, 2011 6:15 AM
  • How come this does not build for me?  I have Win7 and VS2010 and am using C#.  I get the following error:

    'System.Array' does not contain a definition for 'Cast' and no extension method 'Cast' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?)

     

    Wednesday, July 20, 2011 10:47 PM
  • Perhaps it's a Linq extension method? Try "using System.Linq" maybe?

    I think it's this: http://msdn.microsoft.com/en-us/library/bb341406.aspx

    Wednesday, July 20, 2011 11:47 PM