Difference between casting by as and normal cast

G

Guest

I wondering what is the difference between the following two casts

DictionaryEntry entry = obj as DictionaryEntry;
AND

DictionaryEntry entry = (DictionaryEntry)obj;
 
Y

Yves Peneveyre

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

I wondering what is the difference between the following two casts

DictionaryEntry entry = obj as DictionaryEntry;
AND

DictionaryEntry entry = (DictionaryEntry)obj;
 
B

Brock Allen

DictionaryEntry entry = obj as DictionaryEntry;

returns null if cast fails
DictionaryEntry entry = (DictionaryEntry)obj;

throws invalidcastexception when cast fails
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top