Direct casting vs 'as' operator?

后端 未结 16 2094
独厮守ぢ
独厮守ぢ 2020-11-22 01:43

Consider the following code:

void Handler(object o, EventArgs e)
{
   // I swear o is a string
   string s = (string)o; // 1
   //-OR-
   string s = o as str         


        
16条回答
  •  眼角桃花
    2020-11-22 02:17

    Since nobody mentioned it, the closest to instanceOf to Java by keyword is this:

    obj.GetType().IsInstanceOfType(otherObj)
    

提交回复
热议问题