1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-09 23:25:43 +00:00

Null check endPoint (#42)

This commit is contained in:
Oliver Booth 2022-04-21 21:07:47 +01:00
parent a1722e4374
commit 671c08d2e2
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634

View File

@ -19,14 +19,20 @@ public static class EndPointExtensions
/// -or-
/// <para><see cref="string.Empty" /> otherwise.</para>
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="endPoint" /> is <see langword="null" />.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string GetHost(this EndPoint endPoint)
{
if (endPoint is null)
{
throw new ArgumentNullException(nameof(endPoint));
}
return endPoint switch
{
IPEndPoint ip => ip.Address.ToString(),
DnsEndPoint dns => dns.Host,
var _ => string.Empty
_ => string.Empty
};
}
@ -41,14 +47,20 @@ public static class EndPointExtensions
/// -or-
/// <para><c>0</c> otherwise.</para>
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="endPoint" /> is <see langword="null" />.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int GetPort(this EndPoint endPoint)
{
if (endPoint is null)
{
throw new ArgumentNullException(nameof(endPoint));
}
return endPoint switch
{
IPEndPoint ip => ip.Port,
DnsEndPoint dns => dns.Port,
var _ => 0
_ => 0
};
}
}