<p>I'm having a disagreement with someone over how best to implement a simple method that takes an array of integers, and returns the highest integer (using C# 2.0).</p>

<p>Below are the two implementations - I have my own opinion of which is better, and why, but I'd appreciate any impartial opinions.</p>

<p><strong>Option A</strong></p>

<pre><code>public int GetLargestValue(int[] values)
{
   try  {
          Array.Sort(values);
          return values[values.Length - 1];
        }
   catch (Exception){ return -1;}
}
</code></pre>

<p><strong>Option B</strong></p>

<pre><code>public int GetLargestValue(int[] values)
{
    if(values == null)
        return -1;

    if(values.Length &lt; 1)
        return -1;

    int highestValue = values[0];

    foreach(int value in values)
        if(value &gt; highestValue)
            highestValue = value;

    return highestValue;
}
</code></pre>

I prefer
Option B
as it only traverses the collection exactly once.
In
Option A
, you may have to access many elements more than once (the number of times is dependant upon the implementation of the sort alogrithm).
The
Option A
is an inefficent implementation, but results in a fairly clear algorithm.  It does however use a fairly ugly Exception catch which would only ever be triggered if an empty array is passed in (so could probably be written clearer with a pre-sort check).
PS, you should never simply catch "Exception" and then correct things.  There are many types of Exceptions and generally you should catch each possible one and handle accordingly.