Difference between casting by as and normal cast

Discussion in 'ASP .Net' started by =?Utf-8?B?UmFlZCBTYXdhbGhh?=, Aug 15, 2005.

  1. I wondering what is the difference between the following two casts

    DictionaryEntry entry = obj as DictionaryEntry;
    AND

    DictionaryEntry entry = (DictionaryEntry)obj;
     
    =?Utf-8?B?UmFlZCBTYXdhbGhh?=, Aug 15, 2005
    #1
    1. Advertising

  2. Hello !

    The first one will return null if the types are not compatible instead of raising an exception for the second case.

    Best Regards

    Yves Peneveyre


    >>> Raed Sawalha<> 15.08.2005 14:49:03 >>>

    I wondering what is the difference between the following two casts

    DictionaryEntry entry = obj as DictionaryEntry;
    AND

    DictionaryEntry entry = (DictionaryEntry)obj;
     
    Yves Peneveyre, Aug 15, 2005
    #2
    1. Advertising

  3. =?Utf-8?B?UmFlZCBTYXdhbGhh?=

    Brock Allen Guest

    > DictionaryEntry entry = obj as DictionaryEntry;

    returns null if cast fails

    > DictionaryEntry entry = (DictionaryEntry)obj;


    throws invalidcastexception when cast fails

    -Brock
    DevelopMentor
    http://staff.develop.com/ballen
     
    Brock Allen, Aug 15, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jakk
    Replies:
    4
    Views:
    12,612
  2. Johnny
    Replies:
    3
    Views:
    482
    Robert Kern
    Aug 23, 2005
  3. narsing
    Replies:
    2
    Views:
    915
    Thomas Weidenfeller
    Oct 23, 2006
  4. Olaf
    Replies:
    0
    Views:
    659
  5. Masudur
    Replies:
    12
    Views:
    1,048
    Steve C. Orr [MCSD, MVP, CSM, ASP Insider]
    Mar 26, 2007
Loading...

Share This Page