可能重複:
casting vs using the 'as' keyword in the CLR我應該更喜歡'是'還是'as'運營商?
foreach (MyClass i in x)
{
if (i is IMy)
{
IMy a = (IMy)i;
a.M1();
}
}
或
foreach (MyClass i in x)
{
IMy a = i as IMy;
if (a != null)
{
a.M1();
}
}
相關:http://stackoverflow.com/questions/496096/casting-vs-using-the-as-keyword-in-the-clr – JYelton 2011-03-22 20:56:02