Does the List<T> Method. Foreach exist?

Asked

Viewed 191 times

6

I’m trying to implement the example of this page, but VS2015 (.NET 4.5) says the method ForEach() there is no

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);
    }
}

inserir a descrição da imagem aqui

I left the method Print without implementation, obviously the same makes no difference.

2 answers

6

I don’t know if this is your case, but according to this answer from Soen

According to the MS forum:

  • en:

    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 a foreach loop.

    Wes Haggard | . NET Framework Team (BCL) | http://blogs.msdn.com/b/bclteam/

  • pt:

    List.Foreach has been removed in Metro-style apps. While the method looks 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, in which nowhere is there the assertion that this method is not supported in the .NET for Windows Store apps. Maybe this is just an oversight by the documentation team.

  • 1

    That must be it, Console Application works normally, already in the Portable Application nay.

5


Yes, it does. I can’t imagine why this is happening. Although this code does not follow the established standards, it should work.

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

But as it is using Xamarin it may not be available. Xamarin’s documentation says it is, but in . NET documentation does not say it is part of the Portable Class Library.

That has changed , was created the .NET Standard and now o . NET moved to a unique environment.

And the method Print is implemented yes.

  • I’ll try to figure out this ghost bug. Does it have to be a project like Portable Application Xamarin?

  • Certainly that’s it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.