Advantage of "as" operator over casting

  • Thread starter Thread starter Alex
  • Start date Start date
A

Alex

Is that "as" returns null if failed to cast, doesn't throw an exception, so
you can check the reference for null afterwards. straight casting will throw
an InvalidCastException exception.
 
That's right. The ability to convert between compatible types without the
overhead of an exception if the conversion fails.
--------------------
From: "Alex" <[email protected]>
Subject: Advantage of "as" operator over casting
Date: Sat, 2 Aug 2003 16:05:27 -0700
Lines: 5
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <[email protected]>
Newsgroups: microsoft.public.dotnet.languages.csharp
NNTP-Posting-Host: evrtwa1-ar14-4-42-093-032.evrtwa1.dsl-verizon.net 4.42.93.32
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:173771
X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

Is that "as" returns null if failed to cast, doesn't throw an exception, so
you can check the reference for null afterwards. straight casting will throw
an InvalidCastException exception.


--

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which they
originated.
 
Back
Top