Question:Performance-wise, which of the following is the most efficient way to calculate the sum of integers stored in an object array? 

A int FindSum(object[] values) { int sum = 0; foreach (object o in values) { if (o is int) { int x = (int) o; sum += x; } } return sum; } 

B int FindSum (object[] values) { int sum = 0; foreach (object o in values) { int? x = o as int?; if (x.HasValue) { sum += x.Value; } } return sum; } 

C int FindSum (object[] values) { int sum = values.OfType<int>().Sum(); return sum; } 

D int FindSum (object[] values) { int sum = 0; foreach (object o in values) { if (o is int) { int x = Convert.ToInt32(o); sum += x; } } return sum; } 

+ Answer
+ Report
Total Preview: 767

Copyright © 2024. Powered by Intellect Software Ltd