C#, .NET

as vs casting

휘사마 2011. 6. 17. 19:40

1.performance

as 가 더 빠르다고 한다. ( is test 를 포함해도 더 빠른가..??? )

http://www.techgalaxy.net/Docs/Dev/5ways.htm


2.가독성

당연히 as가 더 좋다


3.as의 특징

1) unboxing을 하지 못한다. ( value type -> reference type 변환 불가능)

2) casting 과는 달리 casting 에 실패해도 exception을 throw 하지 않고 null을 리턴한다.

3) user-defined conversion을 하지 못한다.




http://blogs.msdn.com/b/csharpfaq/archive/2004/03/12/what-s-the-difference-between-cast-syntax-and-using-the-code-as-code-operator.aspx