Direct casting vs 'as' operator?

后端 未结 16 1837
独厮守ぢ
独厮守ぢ 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:13

    string s = o as string; // 2
    

    Is prefered, as it avoids the performance penalty of double casting.

提交回复
热议问题