VB.NET Keyword of the Week: DirectCast

I'm starting a new series of blog entries - a weekly keyword in VB.NET that I find interesting, and that some of you may not be aware of.

This weeks word: DirectCast. If you've been using CType to convert values, as in var=CType(expression, typename), consider using DirectCast instead. You can use DirectCast as long as the run-time type of the object variable is the same as the specified type, rather than merely having a valid conversion defined. You'll get better performance. Check out the documentation on DirectCast for more details.

Comments (2)

  1. Or you can see the exact definition when to use what with VB.NET at: http://ipattern.com/simpleblog/PermLink.aspx?entryid=7

    I wrote it up awhile agom after student asked me one simple question.

    I hope you find it useful.


    [www.ipattern.com do you?]

  2. Glen says:

    Thanks, Max. That’s a great write-up you’ve done on conversions.

Skip to main content