code_junkie

Which of these two GetLargestValue C# implementations is better, and why?

Nov 12th, 2011
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.46 KB | None | 0 0
  1. public int GetLargestValue(int[] values)
  2. {
  3. try {
  4. Array.Sort(values);
  5. return values[values.Length - 1];
  6. }
  7. catch (Exception){ return -1;}
  8. }
  9.  
  10. public int GetLargestValue(int[] values)
  11. {
  12. if(values == null)
  13. return -1;
  14.  
  15. if(values.Length < 1)
  16. return -1;
  17.  
  18. int highestValue = values[0];
  19.  
  20. foreach(int value in values)
  21. if(value > highestValue)
  22. highestValue = value;
  23.  
  24. return highestValue;
  25. }
Add Comment
Please, Sign In to add comment