Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / December 2006

Tip: Looking for answers? Try searching our database.

Explicit casting

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Diffident - 27 Nov 2006 19:55 GMT
Hello All,

Is it only a best practice to use "AS" or "IS" or are there any performance
benefits to using these over explicit casting?

Thanks for your clarifications!
Michael Nemtsev - 27 Nov 2006 20:50 GMT
Hello Diffident,

http://groups.google.com/group/microsoft.public.dotnet.languages.csharp/browse_f
rm/thread/5b2c8fda8998aa85/f3ef3e62bf06a929


D> Is it only a best practice to use "AS" or "IS" or are there any
D> performance benefits to using these over explicit casting?

---
WBR,
Michael  Nemtsev [C# MVP] :: blog: http://spaces.live.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
Gabriel Lozano-Morán - 28 Nov 2006 00:17 GMT
The is and as keywords are used for what is called RunTime Type
Identification (RTTI). I recommend Jeffrey Richter's book CLR via C# where
he explains what happens exactly behind the scenes.

Gabriel

> Hello All,
>
[quoted text clipped - 3 lines]
>
> Thanks for your clarifications!
Manish Bafna - 09 Dec 2006 22:34 GMT
Hi,
Yes there are performance benefits of using keywords "as" and "is".Because
keyword "is" does not use relection it is much faster.You will find in detail
about performance benefits of keyword "as" and "is" in the following URL:

http://gabewishnie.blogspot.com/2005/10/is-and-as-lesser-known-c-keywords.html

Thanks and Regards,
manish bafna

> Hello All,
>
> Is it only a best practice to use "AS" or "IS" or are there any performance
> benefits to using these over explicit casting?
>
> Thanks for your clarifications!
Ben Voigt - 11 Dec 2006 18:13 GMT
> Hi,
> Yes there are performance benefits of using keywords "as" and "is".Because
> keyword "is" does not use relection it is much faster.You will find in
> detail

faster than GetType, yes.  Faster than a cast, not unless the cast invokes a
user-defined conversion, in which is/as will fail.

> about performance benefits of keyword "as" and "is" in the following URL:
>
> http://gabewishnie.blogspot.com/2005/10/is-and-as-lesser-known-c-keywords.html

You may want to rely on expert sources.  That blog incorrectly asserts that
"is" and "as" use the same MSIL instruction, whereas there are two separate
instructions "isinst" and "castclass".

In addition, while "is" works perfectly well in the examples shown in that
blog, it is not equivalent.  For example:
object s = "ABC";
s.GetType() == typeof(object); // false
s is object; // true

> Thanks and Regards,
> manish bafna
[quoted text clipped - 6 lines]
>>
>> Thanks for your clarifications!

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.