QHafeez
QHafeez

Reputation: 217

The name "item" does not exist in the current context

I'm learning c# and I've come across a problem trying to use a foreach loop on a List. It keeps saying that "item" does not exist. Here is the code:

 static int SumOf(List<int> nums)
    {

        int total = 0;

        List<int> theList = new List<int>(nums);


        theList.ForEach(int item in theList){

            if (item % 2 == 0)
            {
                total += item;
            }

        }
        return total;


    }

the foreach method is supposed to iterate through the list and add the even numbers to the total.

Upvotes: 0

Views: 3637

Answers (3)

John
John

Reputation: 728

the foreach(type variableName in IEnumerable<T>) and the List<T>.ForEach(Action<T>) is different. the first one is C# syntax for loop any IEnumerable<T> instance (includeing the List<T>), the second one is a method of List<T>. to loop a list ,you can

static int SumOf(List<int> nums)
{
    nums = nums ?? throw new ArgumentNullException(nameof(nums)); // C# 7 syntax for check the nums is null
    int total = 0;
    foreach(var item in nums)
    {
        total += item;
    }
    return toal;
}

Or

static int SumOf(List<int> nums)
{
    nums = nums ?? throw new ArgumentNullException(nameof(nums)); // C# 7 syntax for check the nums is null
    int total = 0;
    nums.ForEach(item=>total += item;)
    return toal;
}

or use the Linq extension method

using System.Linq;

static int SumOf(List<int> nums)
{
    nums = nums ?? throw new ArgumentNullException(nameof(nums)); // C# 7 syntax for check the nums is null
    return nums.Sum()
}

Upvotes: 0

Nam Le
Nam Le

Reputation: 578

The ForEach you are using is a List method that takes an Action.

I think what you want is a foreach loop using foreach keyword

foreach (int item in theList)
{
    if (item % 2 == 0)
    {
        total += item;
    }
}

Of course if you want to do it the ForEach way then:

theList.ForEach((item) =>
    {
        if (item % 2 == 0)
        {
            total += item;
        }
    });

The ForEach method takes an Action that

(Receive an int) => (If it's even, add to total)

and call this Action on every element of the list, so the end result should be the same as using a foreach loop.

Since you're learning C#, I suppose this article is a good read on Actions.

https://learn.microsoft.com/en-us/dotnet/api/system.action?view=netframework-4.8

And here's ForEach's documentation.

https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.foreach?view=netframework-4.8

Upvotes: 4

LazZiya
LazZiya

Reputation: 5719

If you want to use the extension method ForEach you need to use Action, so you directly have access to the list items as below :

theList.ForEach(item => total = item % 2 == 0 ? total + item : total);

or you can use the foreach as below to loop inside the list items:

foreach(var item in theList) 
{
    if (item % 2 == 0)
    {
        total += item;
    }
}

Upvotes: 0

Related Questions