System.InvalidOperationException: Collection was modified; enumeration operation may not execute の原因と解決法
System.InvalidOperationException: Collection was modified; enumeration operation may not execute の原因と具体的な解決手順をまとめました。
エラーメッセージ(全文)
System.InvalidOperationException: Collection was modified; enumeration operation may not execute
このエラーとは
System.InvalidOperationException: Collection was modified; enumeration operation may not execute は C# 開発でよく遭遇するエラーです。
想定環境: Windows / macOS / Linux
よく出る状況: C# / .NET / Unity 演習
よくある原因
- using 不足
- null 参照
- NuGet パッケージ
- ターゲットフレームワーク
解決方法
方法1: CS 番号で検索
Microsoft Docs の CSxxxx を確認.
それでも直らないとき
バージョン情報(C# のバージョン、OS、実行コマンド)を添えて、エラーメッセージ全文と直前に変更した点を確認してください。ログの数行上にも原因の手がかりが残っていることが多いです。
まとめ
「System.InvalidOperationException: Collection was modified; enumeration operation may not execute」は using 不足 が原因のことが多いです。上記の手順で切り分けてください。
次にやること
同じ C# カテゴリの関連エラーもあわせて確認してください。
よくある質問
System.InvalidOperationException: Collection was modified; enumeration operat… とは何ですか?
System.InvalidOperationException: Collection was modified; enumeration operation may not execute は C# 実行時に表示されるエラーです。using 不足
System.InvalidOperationException: Collection was modified; enumeration operat… の原因は?
using 不足。null 参照。NuGet パッケージ。
System.InvalidOperationException: Collection was modified; enumeration operat… の直し方は?
CS 番号で検索:Microsoft Docs の CSxxxx を確認.
C# で System.InvalidOperationException が出るのはなぜ?
C# / .NET / Unity 演習
System.InvalidOperationException: Collection was modified; enumeration operat… を防ぐには?
。
次に読むべき記事
- System.IO.FileNotFoundException: Could not find file 'appsettings.json' の原因と解決法
- System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Projects\app\bin' の原因と解決法
- System.UnauthorizedAccessException: Access to the path 'C:\data\log.txt' is denied の原因と解決法
- System.InvalidOperationException: Sequence contains no elements の原因と解決法
- Shader error in 'Standard': syntax error at line 42 の原因と解決法
同カテゴリ「C#エラー」の記事
- Assets/Scripts/Player.cs(12,9): error CS0103: The name 'transform' does not exist in the current context の原因と解決法
- CS0029: Cannot implicitly convert type 'string' to 'int' in method Process491 の原因と解決法
- Microsoft.EntityFrameworkCore.DbUpdateException: duplicate key value violates unique constraint "IX_Users_Email_488" の原因と解決法
- CS8618: Non-nullable property 'Name483' must contain a non-null value when exiting constructor の原因と解決法
- CS0029: Cannot implicitly convert type 'string' to 'int' in method Process481 の原因と解決法
- Microsoft.EntityFrameworkCore.DbUpdateException: duplicate key value violates unique constraint "IX_Users_Email_478" の原因と解決法
人気記事
- TS2307: Cannot find module '@components/Widget601' or its corresponding type declarations の原因と解決法
- TS2307: Cannot find module '@components/Widget591' or its corresponding type declarations の原因と解決法
- TS2307: Cannot find module '@components/Widget581' or its corresponding type declarations の原因と解決法
- TS2307: Cannot find module '@components/Widget571' or its corresponding type declarations の原因と解決法
- TS2307: Cannot find module '@components/Widget561' or its corresponding type declarations の原因と解決法
- TS2307: Cannot find module '@components/Widget551' or its corresponding type declarations の原因と解決法
学習ルート
体系的に学びたい方はこちらから。
エラー・トラブル集で続きを探す →あわせて読みたい
- System.InvalidOperationException: Sequence contains no elements の原因と解決法同カテゴリの関連エラー
- Shader error in 'Standard': syntax error at line 42 の原因と解決法同カテゴリの関連エラー
- Assets/Scripts/Player.cs(12,9): error CS0103: The name 'transform' does not exist in the current context の原因と解決法同カテゴリの関連エラー
- UnityException: Tag: Player is not defined の原因と解決法同カテゴリの関連エラー
- UnityEngine.MissingReferenceException: The object of type 'GameObject' has been destroyed の原因と解決法同カテゴリの関連エラー