RSS

As, Is Operators and Explicit Conversion, What’s the Difference?

31 Jan

is operator is used to check / verify if an object is compatible with a given type.

int a = 0;
if (a is string) {} // result is false

as operator is used to perform certain types of conversion between compatible, or nullable, types.

as operator will return null type whenever it encounters exception during conversion. as operator will not throw the exception.

int a = 0;
var b = a as string // b is null

Explicit conversion, on the other hand, will throw exception when it encounters one.

int x = 10;
var y = (byte) x; // valid operation
Advertisements
 
Leave a comment

Posted by on January 31, 2013 in General

 

Tags: ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

 
%d bloggers like this: