Direct casting vs 'as' operator?

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

    string s = (string)o; // 1
    

    Throws InvalidCastException if o is not a string. Otherwise, assigns o to s, even if o is null.

    string s = o as string; // 2
    

    Assigns null to s if o is not a string or if o is null. For this reason, you cannot use it with value types (the operator could never return null in that case). Otherwise, assigns o to s.

    string s = o.ToString(); // 3
    

    Causes a NullReferenceException if o is null. Assigns whatever o.ToString() returns to s, no matter what type o is.


    Use 1 for most conversions - it's simple and straightforward. I tend to almost never use 2 since if something is not the right type, I usually expect an exception to occur. I have only seen a need for this return-null type of functionality with badly designed libraries which use error codes (e.g. return null = error, instead of using exceptions).

    3 is not a cast and is just a method invocation. Use it for when you need the string representation of a non-string object.

提交回复
热议问题