Tags
- UI/UX Engineer
- postman collection variables
- Unity
- 좋은 개발자
- LSL_Script
- postman collection
- 프런트엔드
- Interaction developer
- Android
- web developer
- solidity
- 다빈치 리졸브
- oracle
- postman csv
- C++
- postman excel
- postman session
- Java
- MFC
- c#
- Android/iOS Developer
- postman automations
- postman tests
- postman pre-request
- postman
- postman html parse
- Front-end developer
- 우수한 프런트 개발자
- Intellij
- emplace_back
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Archives
- Today
- Total
david's daily developer note
C# Using 문의 try/catch 확장 본문
728x90
using 문의 try - catch 확장
using과 foreach 문을 사용하게 되면 IL 코드로 변환될 때 try/catch 구조로 변환된다. 예를 들어, C# 명세서 8.13 the using statement에 설명된 것을 보자.
using (ResourceType resource = expression) statement
C# 명세서에 따르면 위와 같은 코드에서 ResourceType이 값 타입(value type)이면 다음과 같은 형태로 변환된다.
{
ResourceType resource = expression;
try {
statement;
}
finally {
((IDisposable)resource).Dispose();
}
}
반면에, ResourceType이 참조 타입(reference type)이면 다음과 같다.
{
ResourceType resource = expression;
try {
statement;
}
finally {
if (resource != null) ((IDisposable)resource).Dispose();
}
}
foreach문의 try/catch 확장
C# 명세 8.3.3 foreach statement를 살펴보면 foreach 문에서 사용하는 컬렉션에 따라 그 확장이 달라지게 된다.
foreach (ElementType element in collection)
이와 같은 코드가 있다고 할 때 컬렉션의 형식이 컬렉션 패턴을 구현하고 있다면 다음과 같이 확장된다.
Eenumerator = (collection).GetEnumerator();
try {
while (enumerator.MoveNext()) {
ElementType element = (ElementType)enumerator.Current;
statement;
}
}
finally {
IDisposable disposable = enumerator as System.IDisposable;
if (disposable != null) disposable.Dispose();
}
컬렉션 패턴을 구현하지 않고 IEnumerable을 구현하는 경우에 foreach 확장은 다음과 같이 된다.
IEnumerator enumerator =
((System.Collections.IEnumerable)(collection)).GetEnumerator();
try {
while (enumerator.MoveNext()) {
ElementType element = (ElementType)enumerator.Current;
statement;
}
}
finally {
IDisposable disposable = enumerator as System.IDisposable;
if (disposable != null) disposable.Dispose();
}
저자: 한동훈(traxacun @ unitel.co.kr)
728x90
'[Develop] Language > C#' 카테고리의 다른 글
C# 현재 프로젝트 디렉토리 경로 가져오기. (0) | 2011.01.19 |
---|---|
C# String Format for Double (0) | 2011.01.18 |
C# 다수의 버튼 컨트롤을 동적으로 생성 및 버튼 이동시키는 이벤트 추가하기 (2) | 2010.12.29 |
C#, 이미지 작업. (0) | 2010.12.08 |
.NET 응용 프로그램의 관리자 권한 수행. (0) | 2010.11.05 |