AAsk
AAsk

Reputation: 1491

C# Arrays - Conversion/Copy

I need an array of the same dimensions (to hold values 0 or 1) as an argument to a function which may be of any rank and any type. The result array will contain 0 for failure and 1 for success (I could use Boolean) arising from a process. How can I create the result array?

Upvotes: 0

Views: 64

Answers (1)

Jakub Lortz
Jakub Lortz

Reputation: 14904

Use Array.CreateInstance:

private static Array CreateArray(Array array)
{
    List<int> dimensions = new List<int>();
    for (int i = 0; i < array.Rank; i++)
    {
        dimensions.Add(array.GetLength(i));
    }
    return Array.CreateInstance(typeof(bool), dimensions.ToArray());
}

Upvotes: 5

Related Questions