C# 判斷值類型的幾種方法

object  val

1. if (val.GetType ()==typeof (int))
{
  ........
}

2.  if (val is int)
            {
           
............

    }

3.as

       string s = val as string;

if (s != null)
        {
            Console.WriteLine(s);
        }
        
else
        {
            Console.WriteLine(
"not a string");
        }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章