1

I have a function in C# void func(int x, int y, params object[] arr)

Now this arr can contain anything. For e.g. it can contain an array at [0], a string at [1] and so on.

If I pass the following parameters: func(3, 4, array1, list1, "hello");

array1 is an Array of int, list1 is a List

My question is that how do I extract array1 and list1 from arr and convert it to an Array or List?

6 Answers 6

3
Array myArray = (Array) arr[0];
List<int> myList = (List<int>) arr[1];

or

Array myArray = arr[0] as Array;
List<int> myList = arr[1] as List<int>;
Sign up to request clarification or add additional context in comments.

2 Comments

Conversion is correct but first you have to check if they are an Array or List
Not necessary. Of course it's better safe than sorry, but otherwise it compiles just fine :)
3

You can use Type.IsArray property

public static void func(int x, int y, params object[] arr)
{
    var test = arr.Where(r => r.GetType().IsArray);
}

and you call it like:

 int[] arr = new int[] {1,2,3};
 func(1, 2, 2, 4, arr);

Comments

1

Here is a small program. You have mentioned that the arr can be made of anything, so I presume that on the first index there can be List, array, string or anything else, and that there can be many arrays or lists passed.
Therefore you should probably not presume that on particular indexes you can expect elements of particular type. This solution will extract you all arrays and all lists from arr. Maybe it could be useful for you.

class Program
{
    static void Main(string[] args)
    {
        int[] array1 = new int[10];
        var list1 = new List<int>() { 0, 1, 52 };
        var list2 = new List<string>() { "Mike" };

        func(3, 4, array1, list1, "hello", list2);

    }

    static void func(int x, int y, params object[] arr)
    {
        // Gets all lists and arrays from arr
        IEnumerable<object> listsAndArrays = arr.Where(a => a is IList);
        // Gets all arrays from arr
        IEnumerable<object> arrays = arr.Where(a => a is Array);
        // Gets all lists from arr
        IEnumerable<object> lists = arr.Where(a => a.GetType().IsGenericType && a.GetType().GetGenericTypeDefinition() == typeof(List<>));
    }
}

Comments

1

You can do

void func(int xx, int yy, params object[] arr)
    {
        object value = arr[0];
        Type valueType = value.GetType();
        if (valueType.IsArray)
        {
            Array myArray = (Array)arr[0];
        }

        value = arr[1];
        valueType = value.GetType();
        if (valueType.IsGenericType)
        {
            List<int> myList = (List<int>) arr[1];
        }        
    }

Comments

1
var items = arr.Where(x => x.GetType() == typeof(IEnumerable<object>));
foreach (var item in items)
{
    if (item is List<object>) 
    {
        (item as List<object>).Add("test123");
    }
    else if (item is object[]) {...}
}

Something like this should be a solution.

Comments

0

If I got you right:

    void func(int x, int y, params object[] arr)
    {
        foreach (var a in arr)
        {
            if (a.GetType().IsArray)
            {
                var internalArray = ((Array)a).OfType<object>().ToArray();
                Console.WriteLine(internalArray[0]);
            }
            else if (a is IList)
            {
                var internalList = (IList)a;
                Console.WriteLine(internalList[0]);
            }
        }
    }

1 Comment

The second check: a is IList will be true for arrays and lists, therefore you will have arrays in internalList.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.