Click here to Skip to main content
15,904,288 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have the following data collections:

C#
object data = new object[] { 1, true, "b", "a", false, 1, null };

I would like to get: 2 since 1 + 1 = 2. I was wondering why my code always returns 0;

Any comments and suggestions would be very much appreciated. I am a beginner. Thank you.

What I have tried:

C#
object data = new object[] { 1, true, "b", "a", false, 1, null };
            var objects = new object[] { data };
            int sum = 0;

            for (int i = 0; i < objects.Length; i++)
            {
                foreach (var element in objects)
                {
                    Type tp = element.GetType();

                    if (tp.Equals(typeof(int)))
                    {
                        sum += (int)element;
                    }
                }
            }
            Console.WriteLine(sum);
Posted
Updated 1-Feb-22 7:36am
v2

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:
C#
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[^]
 
Share this answer
 
Comments
CPallini 31-Jan-22 7:39am    
5.
LuckyChloe 31-Jan-22 7:45am    
Thank You Very Much Sir. Your Solution 100% solved my problem. I was just wondering since I was taught that LINQ consumes lot of computational resources, would it be possible to accomplish the same thing with foreach loop?
Richard Deeming 31-Jan-22 7:48am    
Yes, you can use a foreach loop instead. Solution 2 shows you how - although it would still be better to fix the declaration of the data variable, rather than adding the ugly (object[])data cast.
LuckyChloe 31-Jan-22 7:48am    
Another solution provides solution to this problem with foreach loop too so no need to answer my commented question. Thank you very much again
Richard already gave you the elegant solution. Now here you are the clumsy fix for your original code
C#
var data = new object[] { 1, true, "b", "a", false, 1, null };
var objects = new object[][] { data };
int sum = 0;

for (int i = 0; i < objects.Length; i++)
{
  foreach (var element in objects[i])
  {
    if ( element!= null)
	{
	  Type tp = element.GetType();
	
	  if (tp.Equals(typeof(int)))
	  {
	    sum += (int)element;
	  }
    }
  }
}
Console.WriteLine(sum);
 
Share this answer
 
Comments
LuckyChloe 31-Jan-22 7:56am    
I appreciate it. Thank you
CPallini 31-Jan-22 8:02am    
You are welcome.
Patrice T 31-Jan-22 14:22pm    
+5
CPallini 1-Feb-22 2:06am    
Thank you.
Here's another solution for fun...

C#
object[] data = new object[] { 1, true, "b", "a", false, 1, null };
int sum = 0;

foreach (object item in data)
{
    if (item is null) continue;
    
    if (int.TryParse(item.ToString(), out int value))
        sum += value;
}

Console.WriteLine(sum);
 
Share this answer
 
You can also modify you code like:
object data = new object[] { 1, true, "b", "a", false, 1, null};
Object[] data2 =  (Object[])data;
int sum = 0;
foreach (var item in data2)
{
        if (item != null)
        {
                Type tp = item.GetType();
                if (tp.Equals(typeof(int)))
                {
                     sum += (int)item;
                }
        }
}
Console.WriteLine(sum);
 
Share this answer
 
Comments
LuckyChloe 31-Jan-22 7:47am    
This is perfect. Thank you very much. I will reflect on what where I made mistakes.
M Imran Ansari 31-Jan-22 8:07am    
Great. Happy Coding!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900