본문 바로가기

전체 글

(50)
Collection was modified; enumeration operation may not execute. 에러. 리스트 요소 제거 시 주의 할점 보통 foreach문에서 해당 컬렉션을 읽는 도중 수정을 시도할 시 발생하는 에러이다. 수정전: foreach (TMP_InputField input in list) { if (input == null) { list.Remove(input); continue; } inputDict.Add(input.transform.name, input); } 첫번 째 수정: for (int i = 0 ; i < list.Count ; i++) { TMP_InputField input = list[i]; if (input == null) { list.RemoveAt(i); continue; } inputDict.Add(input.transform.name, input); } 하지만 이렇게 작성하면 문제가 발생 할 수 있..
다중 스레드에서 딕셔너리 동시 접근시 에러 InvalidOperationException: Operations that change non-concurrent collections must have exclusive access. A concurrent update was performed on this collection and corrupted its state. The collection's state is no longer correct. InvalidOperationException: 비동시 컬렉션을 변경하는 작업에는 독점 액세스 권한이 있어야 합니다. 이 컬렉션에 대한 동시 업데이트가 수행되어 컬렉션의 상태가 손상되었습니다. 컬렉션의 상태가 더 이상 올바르지 않습니다. 서로 다른 스레드에서 딕셔너리에 동시 접근하면 해당 에러가 발생한다..
Array,List,Dictionary 참조 변경 using System; using System.Collections.Generic; public class Program { public static void Main() { string[] arr = {"a","b","c","d"}; foreach(string str in arr) { Console.Write(str); } Console.WriteLine("--------"); string[] arr2 = {"d","e","f","g", "h", "i"}; arr = arr2; foreach(string str in arr) { Console.Write(str); } } } 결과 : abcd-------- defghi using System; using System.Collections.Generic..