Denise Skidmore
Denise Skidmore

Reputation: 2416

SocketException: ErrorCode vs. SocketErrorCode

Is ErrorCode and SocketErrorCode always the same value but represented as different types?

The SocketException class description gives them mostly identical descriptions, but I don't see any explicit note that they are the same value.

SocketErrorCode seems more useful, since it is an enumeration you can make more beautiful code like:

if(se.SocketErrorCode == SocketError.Interrupted)

rather than:

if (se.ErrorCode != 10004)

or

if (se.ErrorCode != (int)SocketError.Interrupted)

Upvotes: 5

Views: 2523

Answers (1)

Jeroen Vannevel
Jeroen Vannevel

Reputation: 44448

Yes, they are exactly the same.

Looking at 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;
    }
}

Upvotes: 4

Related Questions