Den*_*ore 5 c# sockets error-handling
ErrorCode 和 SocketErrorCode 是否始终相同的值但表示为不同的类型?
SocketException类描述为它们提供了几乎相同的描述,但我没有看到任何明确的说明它们具有相同的值。
SocketErrorCode 似乎更有用,因为它是一个枚举,您可以编写更漂亮的代码,例如:
if(se.SocketErrorCode == SocketError.Interrupted)
Run Code Online (Sandbox Code Playgroud)
而不是:
if (se.ErrorCode != 10004)
Run Code Online (Sandbox Code Playgroud)
或者
if (se.ErrorCode != (int)SocketError.Interrupted)
Run Code Online (Sandbox Code Playgroud)
是的,它们是完全相同的。
查看源代码:
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;
}
}
Run Code Online (Sandbox Code Playgroud)