There are no
int
values in your
objects
array. It contains a single element of type
object[]
.
You're also iterating over the
objects
array twice - once with the
for
loop, and once with the
foreach
loop. You can't currently iterate over the
data
array, since you've declared the variable as
object
instead of
object[]
.
Once you've fixed your variable declaration and removed the unnecessary
objects
array, you can use LINQ to get the sum:
object[] data = new object[] { 1, true, "b", "a", false, 1, null };
int sum = data.OfType<int>().Sum();
Enumerable.OfType<TResult>(IEnumerable) Method (System.Linq) | Microsoft Docs[
^]