views:

65

answers:

3

Hello, There is the next task: I need to check if input parameter (object) is array, then return array of input elements. For example I have input array like this:

int [] array = {1,2,3,4};

And method

  private object[] GetArray(object @from)
  {
  }

So, I need to check in this method that input variable is array and after return this array. For checking I use

if(@from.GetType().IsArray)

And how to create array from object ??? Is it possible ? Thanks.

A: 

you can use arraylist for this task... We can add any object to a arraylist and you can return it then

pankaj
+2  A: 

If what you want to do is return @from as an object[] -- if it is already an object[] -- then the simplest way is just:

private object[] GetArray(object @from)
{
    return @from as object[] ?? new object[] { @from };
}

The above might look kind of confusing. Here's how it works:

  1. If @from is an object[] to begin with, it just returns that (typed as such).
  2. Other wise, the expression @from as object[] evaluates to null. In this case, the null-coalescing operator (??) evaluates the following expression: new object[] { @from }.

So the result is that this method returns either the already existing object[] array, or an array of length 1 containing @from.

On the other hand, if you want to populate an object[] from the contents of @from, I'd do this:

private object[] GetArray(object @from)
{
    var objects = @from as IEnumerable;
    if (objects != null)
        return objects.Cast<object>().ToArray();

    return new object[] { @from };
}

As LukeH pointed out, you could also check to make sure @from is not a string, if you don't want GetArray(string) to return an object[] containing char elements:

private object[] GetArray(object @from)
{
    var str = @from as string;
    if (str != null)
        return new object[] { str };

    var objects = @from as IEnumerable;
    if (objects != null)
        return objects.Cast<object>().ToArray();

    return new object[] { @from };
}
Dan Tao
Thanks a lot...
jitm
+1, but you might want to include a specific check for `string` too, because it also implements `IEnumerable`. (If you pass a `string` to your second version of `GetArray` then it'll return an array of `char`, which probably isn't the desired behaviour.)
LukeH
before do it I'll check like this if(@from.GetType().IsArray)
jitm
@LukeH: Good point. I've added a mention of this to my answer.
Dan Tao
@jitm: You *could* do that, though I would actually not advise it. The reason is that if you had, for example, an `Array` object, the `IsArray` property would return false. (See the remarks in the [MSDN documentation](http://msdn.microsoft.com/en-us/library/system.type.isarray.aspx).) More importantly, it seems to me that to make this method flexible, it ought to deal with any enumerable object (such as a `List<T>`), not just arrays. To otherwise restrict it seems (to me) unnecessary. Granted, I don't know your specific scenario, so maybe it makes sense in your case.
Dan Tao
A: 

Without knowing what the object is, creating an array is a very ambiguous task. What values do you want in the array? Do these values have some deeper meaning. These really are things the object should know about itself and you don't necessarily care to know the details.. For example a Collection has a CopyTo method that performs this task.

unholysampler