Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Assuming that the following are true:</p> <ul> <li><code>valueToCompare</code> is an array <em>of reference types</em> (not, say, an <code>int[]</code> or <code>DateTime[]</code>)</li> <li>The array that <code>valueToCompare</code> is one dimensional (so either declared as a one-dimensional array or a jagged array)</li> </ul> <p>You can cast directly to an <code>object[]</code> instead of <code>Array</code>.</p> <pre><code>bool Compare(object valueToCompare) { Type t = valueToCompare.GetType(); if(t.IsArray() &amp;&amp; !t.GetElementType().IsValueType &amp;&amp; t.GetRank() == 1) { foreach(var value in (valueToCompare as object[])) return Compare(value); } else { return Compare(valueToCompare); } } </code></pre> <p>If your array is of value types, then you're basically out of luck.</p> <p>If your array is multi-dimensional, then you'll just have to write similar checks to account for as many dimensions as you want to support.</p> <p><strong>NOTE:</strong> As you've written it, this function will just call itself recursively (the <code>else</code> branch calls itself with the same parameters) and eventually result in a <code>StackOverflowException</code>. I don't believe this is what you intended to do, but I have left it as-is since I don't know what you <em>actually</em> wanted to do.</p> <p>Additionally this won't actually compile. I'm not sure what your loop is intended to compare; as it is, it will just compare with the first element of the array, but if the array is empty then it won't return a value (hence the compilation error).</p> <p>What are you <em>trying</em> to do here?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload