Bug 24775 - List.ForEach does not throw InvalidOperationException when collection was modified
Summary: List.ForEach does not throw InvalidOperationException when collection was mod...
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: mscorlib (show other bugs)
Version: master
Hardware: All All
: --- normal
Target Milestone: Untriaged
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2014-11-24 11:40 UTC by Andrey Akinshin
Modified: 2014-11-25 04:46 UTC (History)
3 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Andrey Akinshin 2014-11-24 11:40:49 UTC
*** Overview ***

List.ForEach don't throw InvalidOperationException when collection was modified (the behaviour was changed in .NET Framework 4.5).

*** Minimal example ***

using System;
using System.Collections.Generic;

[assembly: global::System.Runtime.Versioning.TargetFrameworkAttribute(".NETFramework,Version=v4.5", FrameworkDisplayName = ".NET Framework 4.5")]

public class Program
{
    public static void Main()
    {
        var list = new List<int> { 1, 2 };
        try
        {
            list.ForEach(i =>
            {
                if (i == 1)
                    list.Add(3);
                Console.WriteLine(i);
            });
        }
        catch (Exception e)
        {
            Console.WriteLine(e.GetType().Name);
        }
    }
}

*** Expected Results ***
1
InvalidOperationException

*** Actual Results ***
1
2
3

*** Useful links ***

.NET Framework Blog: .NET Framework 4.5 – Off to a great start (http://blogs.msdn.com/b/dotnet/archive/2012/10/17/net-framework-4-5-off-to-a-great-start.aspx)
.NET Web Development and Tools Blog: All about : All about <httpRuntime targetFramework> (http://blogs.msdn.com/b/webdev/archive/2012/11/19/all-about-httpruntime-targetframework.aspx)

*** Run details ***
$ uname -srvmpio ; lsb_release -d
Linux 3.13.0-32-generic #57-Ubuntu SMP Tue Jul 15 03:51:08 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
Description:    Ubuntu 14.04.1 LTS

$ mono --version
Mono JIT compiler version 3.10.0 (tarball Mon Oct 27 14:33:41 IST 2014)
Copyright (C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
        TLS:           __thread
        SIGSEGV:       altstack
        Notifications: epoll
        Architecture:  amd64
        Disabled:      none
        Misc:          softdebug 
        LLVM:          supported, not enabled.
        GC:            sgen

$ mcs Program.cs

$ mono Program.exe
1
2
3
Comment 1 Marek Safar 2014-11-25 04:40:35 UTC
Fixed in master