Does the ListT.ForEach Method exist?

5

I'm trying to implement the example of this page , but VS2015 (.NET 4.5) says that the ForEach() method does not exist

class Program
{
    static void Main()
    {
        List<String> names = new List<String>();
        names.Add("Bruce");
        names.Add("Alfred");
        names.Add("Tim");
        names.Add("Richard");

        // Display the contents of the list using the Print method.
        names.ForEach(Print);

        // The following demonstrates the anonymous method feature of C#
        // to display the contents of the list to the console.
        names.ForEach(delegate(String name)
        {
            Console.WriteLine(name);
        });
    }

    private static void Print(string s)
    {
        Console.WriteLine(s);
    }
}

I left the Print method with no implementation, obviously it does not matter.

    
asked by anonymous 15.11.2015 / 02:57

2 answers

4

Yes, there is. See running on dotNetFiddle . I can not imagine why this is happening. Although this code does not follow the established standards, it should work.

But since you are using Xamarin it may not be available. The Xamarin documentation says it is, but in the .Net documentation does not say is part of Portable Class Library .

And the Print method is implemented yes.

    
15.11.2015 / 03:56
5

I do not know if this is the case, but depending on this response from SOen

As per the MS forum :

  
  • in:

         

    List.ForEach has been removed in Metro style apps. While the method seems simple it has a number of potential problems when the list gets mutated by the method passed to ForEach. Instead it is recommended that you simply use the foreach loop.

         

    Wes Haggard | .NET Framework Team (BCL) | link

  •   
  • pt:

         

    List.ForEach has been removed in Metro style apps. While the method seems simple, it has a number of potential problems when the list is modified by the method passed to ForEach. Instead, it is recommended that you simply use a foreach loop.

  •   

However, it does make an appearance in the documentation, where there is nowhere to claim that this method is not supported in .NET for Windows Store apps . Maybe this is just an oversight by the documentation team.

    
15.11.2015 / 04:39