Internally it is equivalent to
expression is type ? (type)expression : (type)null
consider a example,
object [] myobject = new object[5];output
myObjects[0] =
new MyClass2();
myObjects[1] = "hello";
myObjects[2] = 123;
myObjects[3] = 123.4;
myObjects[4] = null;
......
for (int i=0;
i<myObjects.Length; ++i)
{
string s = myObjects[i] as string;
Console.Write ("{0}:", i);
if (s != null)
Console.WriteLine ( "'" + s + "'" );
else
Console.WriteLine ( "not a string" );
}
0:not a string
1:'hello'
2:not a string
3:not a string
4:not a string
No comments:
Post a Comment