As เป็นคำสั่งในการแปลงค่า เหมือนกับ (int)1.35 ซึ่งหากการแปลงค่านั้นไม่สามารถทำได้ จะไดค่าที่เป็น Null ยกตัวอย่าง
class Base
{
public override string ToString()
{
return "Base";
}
}
class Derived : Base
{
}
class Program
{
static void Main()
{
Derived d = new Derived();
Base b = d as Base;
if (b != null)
{
Console.WriteLine(b.ToString());
}
}
}
หรือ
class ClassA { }
class ClassB { }
class MainClass
{
static void Main()
{
object[] objArray = new object[6];
objArray[0] = new ClassA();
objArray[1] = new ClassB();
objArray[2] = "hello";
objArray[3] = 123;
objArray[4] = 123.4;
objArray[5] = null;
for (int i = 0; i < objArray.Length; ++i)
{
string s = objArray[i] as string;
Console.Write("{0}:", i);
if (s != null)
{
Console.WriteLine("'" + s + "'");
}
else
{
Console.WriteLine("not a string");
}
}
}
}
/*
Output:
0:not a string
1:not a string
2:'hello'
3:not a string
4:not a string
5:not a string
*/
ref https://msdn.microsoft.com/en-us/library/cscsdfbt.aspx