성태의 닷넷 이야기
홈 주인
모아 놓은 자료
프로그래밍
질문/답변
사용자 관리
사용자
메뉴
아티클
외부 아티클
유용한 코드
온라인 기능
MathJax 입력기
최근 덧글
[정성태] Roll A Lisp In C - Reading ; https...
[정성태] Java - How to use the Foreign Funct...
[정성태] 제가 큰 실수를 했군요. ^^; Delegate를 통한 Bein...
[정성태] Working with Rust Libraries from C#...
[정성태] Detecting blocking calls using asyn...
[정성태] 아쉽게도, 커뮤니티는 아니고 개인 블로그입니다. ^^
[정성태] 질문이 잘 이해가 안 됩니다. 우선, 해당 소스코드에서 ILis...
[양승조
] var대신 dinamic으로 선언해서 해결은 했습니다. 맞는 해...
[양승조
] 또 막혔습니다. ㅠㅠ var list = props[i].Ge...
[양승조
] 아. 감사합니다. 어제는 안됐던것 같은데....정신을 차려야겠네...
글쓰기
제목
이름
암호
전자우편
HTML
홈페이지
유형
제니퍼 .NET
닷넷
COM 개체 관련
스크립트
VC++
VS.NET IDE
Windows
Team Foundation Server
디버깅 기술
오류 유형
개발 환경 구성
웹
기타
Linux
Java
DDK
Math
Phone
Graphics
사물인터넷
부모글 보이기/감추기
내용
<div style='display: inline'> <h1 style='font-family: Malgun Gothic, Consolas; font-size: 20pt; color: #006699; text-align: center; font-weight: bold'>IEnumerator는 언제나 읽기 전용일까?</h1> <p> 다음과 같은 질문이 있었습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > Foreach loop안에서 generic list에 있는 아이템을 지울수 있나요?? ; <a target='tab' href='http://social.msdn.microsoft.com/Forums/ko-KR/visualcsharpko/thread/8dca9151-15be-403d-91d1-2f6c11758bf0'>http://social.msdn.microsoft.com/Forums/ko-KR/visualcsharpko/thread/8dca9151-15be-403d-91d1-2f6c11758bf0</a> </pre> <br /> 다들, '경험상' 아시겠지만 foreach 내부에서는 컬렉션 목록의 변경(삭제/추가)이 허용되지 않습니다. 그 때문에, 간단하게 그냥 '읽기 전용'이라고 생각하시는 것이 속편합니다.<br /> <br /> 그런데... 사실, '읽기 전용'이라는 것은 구현하기 나름입니다. 즉, 개발자가 구현한 컬렉션에서 GetEnumerator() 결과로 반환하는 인스턴스가 '읽기 전용'이어야 하는 것에 대해서는 개발자 마음이라는 것입니다.<br /> <br /> 닷넷조차도, 일반적인 배열 인스턴스에 대해서는 읽기 전용의 Enumerator 인스턴스를 반환하지는 않습니다. 일례로, 다음과 같은 코드는 오류가 발생하지만,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > ArrayList al = new ArrayList(); al.Add(5); al.Add(6); al.Add(7); al.Add(8); foreach (var aElem in al) { al.Reverse(); // 예외: Collection was modified; enumeration operation may not execute } </pre> <br /> 이를 배열로 바꾸면 오류가 발생하지 않습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > int[] t = new int[] { 5, 6, 7, 8 }; IEnumerator e = t.GetEnumerator(); while (e.MoveNext()) { Console.WriteLine(e.Current); Array.Reverse(t); } // 출력 결과: 5, 7, 7, 5 </pre> <br /> 물론, 출력 결과에 나온 대로 IEnumerator 열람에 대한 결과를 사용자가 예측하기가 쉽지 않기 때문에 '읽기 전용'으로 두는 것이 바람직하다는 것을 직관적으로 알 수 있습니다.<br /> <br /> <hr style='width: 50%' /><br /> <br /> 그런데... 둘 간에는 어떤 차이가 있는 것일까요? 우선, 일반적인 배열에서 반환하는 IEnumerator를 살펴보면, BCL 소스 코드에서 다음과 같이 구현된 것을 확인할 수 있습니다. (닷넷의 모든 배열은 Array 개체입니다.)<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > public abstract class Array { ...[생략]... public IEnumerator GetEnumerator() { int lowerBound = this.GetLowerBound(0); if ((this.Rank == 1) && (lowerBound == 0)) { <span style='color: blue; font-weight: bold'>return new SZArrayEnumerator(this);</span> } return new ArrayEnumerator(this, lowerBound, this.Length); } private sealed class SZArrayEnumerator : IEnumerator, ICloneable { ...[생략]... public bool MoveNext() { if (this._index < this._endIndex) { this._index++; return (this._index < this._endIndex); } return false; } } } </pre> <br /> 보시는 것처럼, SZArrayEnumerator.MoveNext는 인덱스 값만을 증가시킬 뿐 아무런 예외도 발생하지 않습니다.<br /> <br /> 하지만, ArrayList는 다릅니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > public class ArrayList : ...[생략]... { <span style='color: blue; font-weight: bold'>private int _version;</span> ...[생략]... public virtual IEnumerator GetEnumerator() { <span style='color: blue; font-weight: bold'>return new ArrayListEnumeratorSimple(this);</span> } private sealed class ArrayListEnumeratorSimple : IEnumerator, ICloneable { <span style='color: blue; font-weight: bold'>private int version;</span> internal ArrayListEnumeratorSimple(ArrayList list) { ...[생략]... <span style='color: blue; font-weight: bold'>this.version = list._version;</span> } ...[생략]... public bool MoveNext() { <span style='color: blue; font-weight: bold'> if (this.version != this.list._version) { throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumFailedVersion")); }</span> ...[생략]... } } } </pre> <br /> 부가적으로, 컬렉션의 version 필드와 ArrayListEnumeratorSimple 개체가 생성되었을 때 보관한 version 필드의 값을 검사하는 코드가 추가되어 있습니다. 당연히, ArrayList의 Remove/Add 관련 메서드들은 _version 필드에 대한 값을 변경하는 코드를 포함하고 있고!<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > public class ArrayList : ...[생략]... { ...[생략]... <span style='color: blue; font-weight: bold'>public virtual int Add(object value)</span> { if (this._size == this._items.Length) { this.EnsureCapacity(this._size + 1); } this._items[this._size] = value; <span style='color: blue; font-weight: bold'>this._version++;</span> return this._size++; } } </pre> <br /> 마찬가지로, List<T>나 기타 닷넷에 구현된 모든 컬렉션들이 위와 같이 Add/Remove에서 _version 필드를 업데이트하고 있습니다. 여러분들도 역시 자신만의 컬렉션을 만들게 된다면 위와 같이 _version 필드를 두어 '읽기 전용'인 Enumerator 관리를 해주는 것이 좋습니다.<br /> <br /> <hr style='width: 50%' /><br /> <br /> 참고로, 위의 지식을 바탕으로 재미 삼아 ^^ List<T>의 Enumerator 내에서 삭제를 할 수 있는 코드를 다음과 같이 만들어 보는 것도 가능합니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > List<int> intList = new List<int>(); intList.Add(5); intList.Add(6); intList.Add(7); intList.Add(8); <span style='color: blue; font-weight: bold'>IEnumerator eList = intList.GetEnumerator(); while (eList.MoveNext()) { intList.Remove((int)eList.Current); </span> DecrementField(eList, "index"); DecrementField(intList, "_version"); } <span style='color: blue; font-weight: bold'>Console.WriteLine(intList.Count); // 출력 결과: 0</span> private static void DecrementField(object objTarget, string fieldName) { FieldInfo versionField = objTarget.GetType().GetField(fieldName, BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance); int current = (int)versionField.GetValue(objTarget); versionField.SetValue(objTarget, current - 1); } </pre> <br /> 물론, 위와 같이 구현하느니... 차라리 intList.Clear() 하는 것이 더 낫습니다. ^^<br /> </p><br /> <br /><hr /><span style='color: Maroon'>[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]</span> </div>
첨부파일
스팸 방지용 인증 번호
2019
(왼쪽의 숫자를 입력해야 합니다.)