+-
C#-SocketException:ErrorCode与SocketErrorCode
ErrorCode和SocketErrorCode是否总是相同的值,但表示为不同的类型?

SocketException class description给出了与它们大致相同的描述,但是我没有看到任何明确的说明它们具有相同的值.

SocketErrorCode似乎更有用,因为它是一个枚举,因此您可以编写更漂亮的代码,例如:

if(se.SocketErrorCode == SocketError.Interrupted)

而不是:

if (se.ErrorCode != 10004)

要么

if (se.ErrorCode != (int)SocketError.Interrupted)
最佳答案
是的,它们是完全一样的.

看the source code:

public override int ErrorCode {
    //
    // the base class returns the HResult with this property
    // we need the Win32 Error Code, hence the override.
    //
    get {
        return NativeErrorCode;
    }
}

public SocketError SocketErrorCode {
    //
    // the base class returns the HResult with this property
    // we need the Win32 Error Code, hence the override.
    //
    get {
        return (SocketError)NativeErrorCode;
    }
}
点击查看更多相关文章

转载注明原文:C#-SocketException:ErrorCode与SocketErrorCode - 乐贴网