Bug 24775

Summary: List.ForEach does not throw InvalidOperationException when collection was modified
Product: [Mono] Class Libraries Reporter: Andrey Akinshin <andrey.akinshin>
Component: mscorlibAssignee: Marek Safar <masafa>
Status: RESOLVED FIXED    
Severity: normal CC: andrey.akinshin, masafa, mono-bugs+mono
Priority: ---    
Version: master   
Target Milestone: Untriaged   
Hardware: All   
OS: All   
Tags: Is this bug a regression?: ---
Last known good build:

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