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. Advertisements

  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. Advertisements

  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. Advertisements

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:
    13,974
  2. Private
    Replies:
    2
    Views:
    722
    Michael Borgwardt
    Dec 11, 2004
  3. Santa
    Replies:
    1
    Views:
    1,459
    Mark A. Odell
    Jul 17, 2003
  4. Johnny
    Replies:
    3
    Views:
    699
    Robert Kern
    Aug 23, 2005
  5. narsing
    Replies:
    2
    Views:
    1,120
    Thomas Weidenfeller
    Oct 23, 2006
  6. infobahn
    Replies:
    1
    Views:
    488
    Randy Howard
    Feb 15, 2005
  7. Nishu
    Replies:
    6
    Views:
    446
    Serve Laurijssen
    Feb 17, 2007
  8. Masudur
    Replies:
    12
    Views:
    1,276
    Steve C. Orr [MCSD, MVP, CSM, ASP Insider]
    Mar 26, 2007
Loading...