Is that a VB code or C#?

Ok, so the below code got me confused for a moment and I had to verify a couple of times if I am in the right language environment :).

int myNumber = 1;
string myString = myNumber as string;

 

My initial question is, what’s the as keyword doing in a C# code?  Is that a variable declaration like in VB?  As it turned out, it’s another operator in C# that performs like a cast operation but instead of raising an exception if the conversion is not possible, it returns null.  So there you go, next time you see as keyword in C#, you are not hallucinating :).

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s