🧹 Code style cleanup

This commit is contained in:
Oliver Booth 2020-07-15 14:31:02 +01:00
parent e747921fb7
commit f8541fd239
23 changed files with 813 additions and 907 deletions

View File

@ -1,3 +1,7 @@
# All files
[*]
guidelines = 130
# C# files
[*.cs]
@ -61,9 +65,9 @@ dotnet_code_quality_unused_parameters = all:suggestion
#### C# Coding Conventions ####
# var preferences
csharp_style_var_elsewhere = false:warning
csharp_style_var_for_built_in_types = true:warning
csharp_style_var_elsewhere=true:warning
csharp_style_var_when_type_is_apparent=true:warning
csharp_style_var_for_built_in_types=true:warning
# Expression-bodied members
csharp_style_expression_bodied_accessors=true:suggestion
@ -72,7 +76,7 @@ csharp_style_expression_bodied_indexers = true:suggestion
csharp_style_expression_bodied_lambdas=true:silent
csharp_style_expression_bodied_local_functions=false:silent
csharp_style_expression_bodied_methods=false:suggestion
csharp_style_expression_bodied_operators = true:suggestion
csharp_style_expression_bodied_operators=false:suggestion
csharp_style_expression_bodied_properties=false:suggestion
# Pattern matching preferences
@ -107,6 +111,8 @@ csharp_using_directive_placement = inside_namespace:suggestion
#### C# Formatting Rules ####
max_line_length=130
# New line preferences
csharp_new_line_before_catch=true
csharp_new_line_before_else=true
@ -193,3 +199,17 @@ dotnet_naming_style.begins_with_i.required_prefix = I
dotnet_naming_style.begins_with_i.required_suffix=
dotnet_naming_style.begins_with_i.word_separator=
dotnet_naming_style.begins_with_i.capitalization=pascal_case
# XML comment analysis is disabled due to project configuration
# https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA0001.md
dotnet_diagnostic.SA0001.severity = silent
# A C# code file is missing a standard file header.
# https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1633.md
dotnet_diagnostic.SA1633.severity = silent
# Consider calling ConfigureAwait on the awaited Task
# https://docs.microsoft.com/en-us/visualstudio/code-quality/ca2007?view=vs-2019
# https://github.com/JosefPihrt/Roslynator/blob/master/docs/analyzers/RCS1090.md
dotnet_diagnostic.CA2007.severity = silent
dotnet_diagnostic.RCS1090.severity = silent

View File

@ -1,259 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<RuleSet Name="X10D" Description=" " ToolsVersion="16.0">
<Rules AnalyzerId="Microsoft.Analyzers.ManagedCodeAnalysis" RuleNamespace="Microsoft.Rules.Managed">
<Rule Id="CA1000" Action="Warning" />
<Rule Id="CA1001" Action="Warning" />
<Rule Id="CA1002" Action="Warning" />
<Rule Id="CA1003" Action="Warning" />
<Rule Id="CA1004" Action="Warning" />
<Rule Id="CA1005" Action="Warning" />
<Rule Id="CA1006" Action="Warning" />
<Rule Id="CA1007" Action="Warning" />
<Rule Id="CA1008" Action="Warning" />
<Rule Id="CA1009" Action="Warning" />
<Rule Id="CA1010" Action="Warning" />
<Rule Id="CA1011" Action="Warning" />
<Rule Id="CA1012" Action="Warning" />
<Rule Id="CA1013" Action="Warning" />
<Rule Id="CA1014" Action="Warning" />
<Rule Id="CA1016" Action="Warning" />
<Rule Id="CA1017" Action="Warning" />
<Rule Id="CA1018" Action="Warning" />
<Rule Id="CA1019" Action="Warning" />
<Rule Id="CA1020" Action="Warning" />
<Rule Id="CA1021" Action="Warning" />
<Rule Id="CA1023" Action="Warning" />
<Rule Id="CA1024" Action="Warning" />
<Rule Id="CA1025" Action="Warning" />
<Rule Id="CA1026" Action="Warning" />
<Rule Id="CA1027" Action="Warning" />
<Rule Id="CA1028" Action="Warning" />
<Rule Id="CA1030" Action="Warning" />
<Rule Id="CA1031" Action="Warning" />
<Rule Id="CA1032" Action="Warning" />
<Rule Id="CA1033" Action="Warning" />
<Rule Id="CA1034" Action="Warning" />
<Rule Id="CA1035" Action="Warning" />
<Rule Id="CA1036" Action="Warning" />
<Rule Id="CA1038" Action="Warning" />
<Rule Id="CA1039" Action="Warning" />
<Rule Id="CA1040" Action="Warning" />
<Rule Id="CA1041" Action="Warning" />
<Rule Id="CA1043" Action="Warning" />
<Rule Id="CA1044" Action="Warning" />
<Rule Id="CA1045" Action="Warning" />
<Rule Id="CA1046" Action="Warning" />
<Rule Id="CA1047" Action="Warning" />
<Rule Id="CA1048" Action="Warning" />
<Rule Id="CA1049" Action="Warning" />
<Rule Id="CA1050" Action="Warning" />
<Rule Id="CA1051" Action="Warning" />
<Rule Id="CA1052" Action="Warning" />
<Rule Id="CA1053" Action="Warning" />
<Rule Id="CA1054" Action="Warning" />
<Rule Id="CA1055" Action="Warning" />
<Rule Id="CA1056" Action="Warning" />
<Rule Id="CA1057" Action="Warning" />
<Rule Id="CA1058" Action="Warning" />
<Rule Id="CA1059" Action="Warning" />
<Rule Id="CA1060" Action="Warning" />
<Rule Id="CA1061" Action="Warning" />
<Rule Id="CA1062" Action="Warning" />
<Rule Id="CA1063" Action="Warning" />
<Rule Id="CA1064" Action="Warning" />
<Rule Id="CA1065" Action="Warning" />
<Rule Id="CA1300" Action="Warning" />
<Rule Id="CA1301" Action="Warning" />
<Rule Id="CA1302" Action="Warning" />
<Rule Id="CA1303" Action="Warning" />
<Rule Id="CA1304" Action="Warning" />
<Rule Id="CA1305" Action="Warning" />
<Rule Id="CA1306" Action="Warning" />
<Rule Id="CA1307" Action="Warning" />
<Rule Id="CA1308" Action="Warning" />
<Rule Id="CA1309" Action="Warning" />
<Rule Id="CA1400" Action="Warning" />
<Rule Id="CA1401" Action="Warning" />
<Rule Id="CA1402" Action="Warning" />
<Rule Id="CA1403" Action="Warning" />
<Rule Id="CA1404" Action="Warning" />
<Rule Id="CA1405" Action="Warning" />
<Rule Id="CA1406" Action="Warning" />
<Rule Id="CA1407" Action="Warning" />
<Rule Id="CA1408" Action="Warning" />
<Rule Id="CA1409" Action="Warning" />
<Rule Id="CA1410" Action="Warning" />
<Rule Id="CA1411" Action="Warning" />
<Rule Id="CA1412" Action="Warning" />
<Rule Id="CA1413" Action="Warning" />
<Rule Id="CA1414" Action="Warning" />
<Rule Id="CA1415" Action="Warning" />
<Rule Id="CA1500" Action="Warning" />
<Rule Id="CA1501" Action="Warning" />
<Rule Id="CA1502" Action="Warning" />
<Rule Id="CA1504" Action="Warning" />
<Rule Id="CA1505" Action="Warning" />
<Rule Id="CA1506" Action="Warning" />
<Rule Id="CA1600" Action="Warning" />
<Rule Id="CA1601" Action="Warning" />
<Rule Id="CA1700" Action="Warning" />
<Rule Id="CA1701" Action="Warning" />
<Rule Id="CA1702" Action="Warning" />
<Rule Id="CA1703" Action="Warning" />
<Rule Id="CA1704" Action="Warning" />
<Rule Id="CA1707" Action="Warning" />
<Rule Id="CA1708" Action="Warning" />
<Rule Id="CA1709" Action="Warning" />
<Rule Id="CA1710" Action="Warning" />
<Rule Id="CA1711" Action="Warning" />
<Rule Id="CA1712" Action="Warning" />
<Rule Id="CA1713" Action="Warning" />
<Rule Id="CA1714" Action="Warning" />
<Rule Id="CA1715" Action="Warning" />
<Rule Id="CA1716" Action="Warning" />
<Rule Id="CA1717" Action="Warning" />
<Rule Id="CA1719" Action="Warning" />
<Rule Id="CA1720" Action="Warning" />
<Rule Id="CA1721" Action="Warning" />
<Rule Id="CA1722" Action="Warning" />
<Rule Id="CA1724" Action="Warning" />
<Rule Id="CA1725" Action="Warning" />
<Rule Id="CA1726" Action="Warning" />
<Rule Id="CA1800" Action="Warning" />
<Rule Id="CA1801" Action="Warning" />
<Rule Id="CA1802" Action="Warning" />
<Rule Id="CA1804" Action="Warning" />
<Rule Id="CA1806" Action="Warning" />
<Rule Id="CA1809" Action="Warning" />
<Rule Id="CA1810" Action="Warning" />
<Rule Id="CA1811" Action="Warning" />
<Rule Id="CA1812" Action="Warning" />
<Rule Id="CA1813" Action="Warning" />
<Rule Id="CA1814" Action="Warning" />
<Rule Id="CA1815" Action="Warning" />
<Rule Id="CA1816" Action="Warning" />
<Rule Id="CA1819" Action="Warning" />
<Rule Id="CA1820" Action="Warning" />
<Rule Id="CA1821" Action="Warning" />
<Rule Id="CA1822" Action="Warning" />
<Rule Id="CA1823" Action="Warning" />
<Rule Id="CA1824" Action="Warning" />
<Rule Id="CA1900" Action="Warning" />
<Rule Id="CA1901" Action="Warning" />
<Rule Id="CA1903" Action="Warning" />
<Rule Id="CA2000" Action="Warning" />
<Rule Id="CA2001" Action="Warning" />
<Rule Id="CA2002" Action="Warning" />
<Rule Id="CA2003" Action="Warning" />
<Rule Id="CA2004" Action="Warning" />
<Rule Id="CA2006" Action="Warning" />
<Rule Id="CA2100" Action="Warning" />
<Rule Id="CA2101" Action="Warning" />
<Rule Id="CA2102" Action="Warning" />
<Rule Id="CA2103" Action="Warning" />
<Rule Id="CA2104" Action="Warning" />
<Rule Id="CA2105" Action="Warning" />
<Rule Id="CA2106" Action="Warning" />
<Rule Id="CA2107" Action="Warning" />
<Rule Id="CA2108" Action="Warning" />
<Rule Id="CA2109" Action="Warning" />
<Rule Id="CA2111" Action="Warning" />
<Rule Id="CA2112" Action="Warning" />
<Rule Id="CA2114" Action="Warning" />
<Rule Id="CA2115" Action="Warning" />
<Rule Id="CA2116" Action="Warning" />
<Rule Id="CA2117" Action="Warning" />
<Rule Id="CA2118" Action="Warning" />
<Rule Id="CA2119" Action="Warning" />
<Rule Id="CA2120" Action="Warning" />
<Rule Id="CA2121" Action="Warning" />
<Rule Id="CA2122" Action="Warning" />
<Rule Id="CA2123" Action="Warning" />
<Rule Id="CA2124" Action="Warning" />
<Rule Id="CA2126" Action="Warning" />
<Rule Id="CA2130" Action="Warning" />
<Rule Id="CA2131" Action="Warning" />
<Rule Id="CA2132" Action="Warning" />
<Rule Id="CA2133" Action="Warning" />
<Rule Id="CA2134" Action="Warning" />
<Rule Id="CA2135" Action="Warning" />
<Rule Id="CA2136" Action="Warning" />
<Rule Id="CA2137" Action="Warning" />
<Rule Id="CA2138" Action="Warning" />
<Rule Id="CA2139" Action="Warning" />
<Rule Id="CA2140" Action="Warning" />
<Rule Id="CA2141" Action="Warning" />
<Rule Id="CA2142" Action="Warning" />
<Rule Id="CA2143" Action="Warning" />
<Rule Id="CA2144" Action="Warning" />
<Rule Id="CA2145" Action="Warning" />
<Rule Id="CA2146" Action="Warning" />
<Rule Id="CA2147" Action="Warning" />
<Rule Id="CA2149" Action="Warning" />
<Rule Id="CA2151" Action="Warning" />
<Rule Id="CA2200" Action="Warning" />
<Rule Id="CA2201" Action="Warning" />
<Rule Id="CA2202" Action="Warning" />
<Rule Id="CA2204" Action="Warning" />
<Rule Id="CA2205" Action="Warning" />
<Rule Id="CA2207" Action="Warning" />
<Rule Id="CA2208" Action="Warning" />
<Rule Id="CA2210" Action="Warning" />
<Rule Id="CA2211" Action="Warning" />
<Rule Id="CA2212" Action="Warning" />
<Rule Id="CA2213" Action="Warning" />
<Rule Id="CA2214" Action="Warning" />
<Rule Id="CA2215" Action="Warning" />
<Rule Id="CA2216" Action="Warning" />
<Rule Id="CA2217" Action="Warning" />
<Rule Id="CA2218" Action="Warning" />
<Rule Id="CA2219" Action="Warning" />
<Rule Id="CA2220" Action="Warning" />
<Rule Id="CA2221" Action="Warning" />
<Rule Id="CA2222" Action="Warning" />
<Rule Id="CA2223" Action="Warning" />
<Rule Id="CA2224" Action="Warning" />
<Rule Id="CA2225" Action="Warning" />
<Rule Id="CA2226" Action="Warning" />
<Rule Id="CA2227" Action="Warning" />
<Rule Id="CA2228" Action="Warning" />
<Rule Id="CA2229" Action="Warning" />
<Rule Id="CA2230" Action="Warning" />
<Rule Id="CA2231" Action="Warning" />
<Rule Id="CA2232" Action="Warning" />
<Rule Id="CA2233" Action="Warning" />
<Rule Id="CA2234" Action="Warning" />
<Rule Id="CA2235" Action="Warning" />
<Rule Id="CA2236" Action="Warning" />
<Rule Id="CA2237" Action="Warning" />
<Rule Id="CA2238" Action="Warning" />
<Rule Id="CA2239" Action="Warning" />
<Rule Id="CA2240" Action="Warning" />
<Rule Id="CA2241" Action="Warning" />
<Rule Id="CA2242" Action="Warning" />
<Rule Id="CA2243" Action="Warning" />
<Rule Id="CA5122" Action="Warning" />
</Rules>
<Rules AnalyzerId="Microsoft.CodeAnalysis.CSharp.Features" RuleNamespace="Microsoft.CodeAnalysis.CSharp.Features">
<Rule Id="IDE0008" Action="None" />
<Rule Id="IDE0008WithoutSuggestion" Action="None" />
</Rules>
<Rules AnalyzerId="Microsoft.NetCore.Analyzers" RuleNamespace="Microsoft.NetCore.Analyzers">
<Rule Id="CA1813" Action="Warning" />
</Rules>
<Rules AnalyzerId="Roslynator.CSharp.Analyzers" RuleNamespace="Roslynator.CSharp.Analyzers">
<Rule Id="RCS1010" Action="Warning" />
<Rule Id="RCS1177" Action="Warning" />
</Rules>
<Rules AnalyzerId="StyleCop.Analyzers" RuleNamespace="StyleCop.Analyzers">
<Rule Id="SA1121" Action="None" />
<Rule Id="SA1633" Action="None" />
<Rule Id="SA1634" Action="None" />
<Rule Id="SA1635" Action="None" />
<Rule Id="SA1636" Action="None" />
<Rule Id="SA1637" Action="None" />
<Rule Id="SA1638" Action="None" />
<Rule Id="SA1640" Action="None" />
<Rule Id="SA1641" Action="None" />
</Rules>
</RuleSet>

View File

@ -10,8 +10,10 @@
/// </summary>
/// <param name="value">The boolean.</param>
/// <param name="comparison">The boolean comparator.</param>
/// <returns>Returns <see langword="true"/> if <paramref name="value"/> AND <paramref name="comparison"/>
/// evaluate to <see langword="true"/>, or <see langword="false"/> otherwise.</returns>
/// <returns>
/// Returns <see langword="true" /> if <paramref name="value" /> AND <paramref name="comparison" />
/// evaluate to <see langword="true" />, or <see langword="false" /> otherwise.
/// </returns>
public static bool And(this bool value, bool comparison)
{
return value && comparison;
@ -22,8 +24,10 @@
/// </summary>
/// <param name="value">The boolean.</param>
/// <param name="comparison">The boolean comparator.</param>
/// <returns>Returns <see langword="true"/> if <paramref name="value"/> NAND <paramref name="comparison"/>
/// evaluate to <see langword="true"/>, or <see langword="false"/> otherwise.</returns>
/// <returns>
/// Returns <see langword="true" /> if <paramref name="value" /> NAND <paramref name="comparison" />
/// evaluate to <see langword="true" />, or <see langword="false" /> otherwise.
/// </returns>
public static bool NAnd(this bool value, bool comparison)
{
return !(value && comparison);
@ -34,8 +38,10 @@
/// </summary>
/// <param name="value">The boolean.</param>
/// <param name="comparison">The boolean comparator.</param>
/// <returns>Returns <see langword="true"/> if <paramref name="value"/> NOR <paramref name="comparison"/>
/// evaluate to <see langword="true"/>, or <see langword="false"/> otherwise.</returns>
/// <returns>
/// Returns <see langword="true" /> if <paramref name="value" /> NOR <paramref name="comparison" />
/// evaluate to <see langword="true" />, or <see langword="false" /> otherwise.
/// </returns>
public static bool NOr(this bool value, bool comparison)
{
return !(value || comparison);
@ -45,8 +51,10 @@
/// Performs logical NOT on this <see cref="bool" />.
/// </summary>
/// <param name="value">The boolean.</param>
/// <returns>Returns <see langword="true"/> if <paramref name="value"/> is <see langword="false"/>,
/// or <see langword="false"/> otherwise.</returns>
/// <returns>
/// Returns <see langword="true" /> if <paramref name="value" /> is <see langword="false" />,
/// or <see langword="false" /> otherwise.
/// </returns>
public static bool Not(this bool value)
{
return !value;
@ -57,8 +65,10 @@
/// </summary>
/// <param name="value">The boolean.</param>
/// <param name="comparison">The boolean comparator.</param>
/// <returns>Returns <see langword="true"/> if <paramref name="value"/> OR <paramref name="comparison"/>
/// evaluate to <see langword="true"/>, or <see langword="false"/> otherwise.</returns>
/// <returns>
/// Returns <see langword="true" /> if <paramref name="value" /> OR <paramref name="comparison" />
/// evaluate to <see langword="true" />, or <see langword="false" /> otherwise.
/// </returns>
public static bool Or(this bool value, bool comparison)
{
return value || comparison;
@ -109,8 +119,10 @@
/// </summary>
/// <param name="value">The boolean.</param>
/// <param name="comparison">The boolean comparator.</param>
/// <returns>Returns <see langword="true"/> if <paramref name="value"/> XNOR <paramref name="comparison"/>
/// evaluate to <see langword="true"/>, or <see langword="false"/> otherwise.</returns>
/// <returns>
/// Returns <see langword="true" /> if <paramref name="value" /> XNOR <paramref name="comparison" />
/// evaluate to <see langword="true" />, or <see langword="false" /> otherwise.
/// </returns>
public static bool XNOr(this bool value, bool comparison)
{
return !(value ^ comparison);
@ -121,8 +133,10 @@
/// </summary>
/// <param name="value">The boolean.</param>
/// <param name="comparison">The boolean comparator.</param>
/// <returns>Returns <see langword="true"/> if <paramref name="value"/> XOR <paramref name="comparison"/>
/// evaluate to <see langword="true"/>, or <see langword="false"/> otherwise.</returns>
/// <returns>
/// Returns <see langword="true" /> if <paramref name="value" /> XOR <paramref name="comparison" />
/// evaluate to <see langword="true" />, or <see langword="false" /> otherwise.
/// </returns>
public static bool XOr(this bool value, bool comparison)
{
return value ^ comparison;

View File

@ -50,6 +50,36 @@
return BitConverter.ToInt64(bytes.ToArray(), 0);
}
/// <summary>
/// Gets a <see cref="string" /> representing the value the <see cref="byte" />[] with
/// <see cref="Encoding.UTF8" /> encoding.
/// </summary>
/// <param name="bytes">The bytes to convert.</param>
/// <returns>Returns a <see cref="string" />.</returns>
public static string GetString(this IEnumerable<byte> bytes)
{
return bytes.GetString(Encoding.UTF8);
}
/// <summary>
/// Gets a <see cref="string" /> representing the value the <see cref="byte" />[] with the provided encoding.
/// </summary>
/// <param name="bytes">The bytes to convert.</param>
/// <param name="encoding">The encoding to use.</param>
/// <returns>Returns a <see cref="string" />.</returns>
/// <exception cref="ArgumentNullException"><paramref name="encoding" /> is <see langword="null" />.</exception>
public static string GetString(this IEnumerable<byte> bytes, Encoding encoding)
{
if (encoding is null)
{
throw new ArgumentNullException(nameof(encoding));
}
// ReSharper disable once SuggestVarOrType_Elsewhere
var array = bytes.ToArray();
return encoding.GetString(array, 0, array.Length);
}
/// <summary>
/// Converts the <see cref="byte" />[] to a <see cref="ushort" />.
/// </summary>
@ -82,35 +112,5 @@
{
return BitConverter.ToUInt64(bytes.ToArray(), 0);
}
/// <summary>
/// Gets a <see cref="string"/> representing the value the <see cref="byte"/>[] with
/// <see cref="Encoding.UTF8"/> encoding.
/// </summary>
/// <param name="bytes">The bytes to convert.</param>
/// <returns>Returns a <see cref="string"/>.</returns>
public static string GetString(this IEnumerable<byte> bytes)
{
return bytes.GetString(Encoding.UTF8);
}
/// <summary>
/// Gets a <see cref="string"/> representing the value the <see cref="byte"/>[] with the provided encoding.
/// </summary>
/// <param name="bytes">The bytes to convert.</param>
/// <param name="encoding">The encoding to use.</param>
/// <returns>Returns a <see cref="string"/>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="encoding"/> is <see langword="null"/>.</exception>
public static string GetString(this IEnumerable<byte> bytes, Encoding encoding)
{
if (encoding is null)
{
throw new ArgumentNullException(nameof(encoding));
}
// ReSharper disable once SuggestVarOrType_Elsewhere
var array = bytes.ToArray();
return encoding.GetString(array, 0, array.Length);
}
}
}

View File

@ -78,8 +78,10 @@
/// </summary>
/// <param name="c">The character to repeat.</param>
/// <param name="count">The repeat count.</param>
/// <returns>Returns a <see cref="string"/> whose value is <paramref name="c"/> repeated
/// <paramref name="count"/> times.</returns>
/// <returns>
/// Returns a <see cref="string" /> whose value is <paramref name="c" /> repeated
/// <paramref name="count" /> times.
/// </returns>
public static string Repeat(this char c, int count)
{
return new string(c, count);

View File

@ -14,8 +14,10 @@
/// <param name="actual">The value to compare.</param>
/// <param name="lower">The exclusive lower bound.</param>
/// <param name="upper">The exclusive upper bound.</param>
/// <returns>Returns <see langword="true"/> if the value is between the bounds, <see langword="false"/>
/// otherwise.</returns>
/// <returns>
/// Returns <see langword="true" /> if the value is between the bounds, <see langword="false" />
/// otherwise.
/// </returns>
public static bool Between<T>(this T actual, T lower, T upper)
where T : IComparable<T>
{

View File

@ -14,9 +14,11 @@
/// <param name="value">The object to convert.</param>
/// <param name="provider">An object that supplies culture-specific formatting information.</param>
/// <returns>Returns the value converted to <see cref="T" />.</returns>
/// <exception cref="InvalidCastException">This conversion is not supported.
/// <exception cref="InvalidCastException">
/// This conversion is not supported.
/// -or-
/// <paramref name="value"/> is <see langword="null"/> and <typeparamref name="T"/> is a value type.</exception>
/// <paramref name="value" /> is <see langword="null" /> and <typeparamref name="T" /> is a value type.
/// </exception>
[CLSCompliant(false)]
public static T To<T>(this IConvertible value, IFormatProvider provider = null)
{
@ -71,6 +73,35 @@
}
}
/// <summary>
/// Converts the object to another type, returning <see langword="null" /> on failure.
/// </summary>
/// <typeparam name="T">The type to convert to.</typeparam>
/// <param name="value">The object to convert.</param>
/// <param name="provider">An object that supplies culture-specific formatting information.</param>
/// <returns>Returns a <see cref="T" /> or <see langword="null" />.</returns>
[CLSCompliant(false)]
public static T ToOrNull<T>(this IConvertible value, IFormatProvider provider = null)
where T : class
{
return value.ToOrNull(out T v, provider) ? v : null;
}
/// <summary>
/// Converts the object to another type, returning <see langword="null" /> on failure.
/// </summary>
/// <typeparam name="T">The type to convert to.</typeparam>
/// <param name="value">The object to convert.</param>
/// <param name="newObj">The parameter where the result should be sent.</param>
/// <param name="provider">An object that supplies culture-specific formatting information.</param>
/// <returns>Returns a <see cref="T" /> or <see langword="null" />.</returns>
[CLSCompliant(false)]
public static bool ToOrNull<T>(this IConvertible value, out T newObj, IFormatProvider provider = null)
where T : class
{
return ToOrOther(value, out newObj, null, provider);
}
/// <summary>
/// Converts the object to another type, returning a different value on failure.
/// </summary>
@ -126,34 +157,5 @@
return false;
}
}
/// <summary>
/// Converts the object to another type, returning <see langword="null"/> on failure.
/// </summary>
/// <typeparam name="T">The type to convert to.</typeparam>
/// <param name="value">The object to convert.</param>
/// <param name="provider">An object that supplies culture-specific formatting information.</param>
/// <returns>Returns a <see cref="T"/> or <see langword="null"/>.</returns>
[CLSCompliant(false)]
public static T ToOrNull<T>(this IConvertible value, IFormatProvider provider = null)
where T : class
{
return value.ToOrNull(out T v, provider) ? v : null;
}
/// <summary>
/// Converts the object to another type, returning <see langword="null"/> on failure.
/// </summary>
/// <typeparam name="T">The type to convert to.</typeparam>
/// <param name="value">The object to convert.</param>
/// <param name="newObj">The parameter where the result should be sent.</param>
/// <param name="provider">An object that supplies culture-specific formatting information.</param>
/// <returns>Returns a <see cref="T"/> or <see langword="null"/>.</returns>
[CLSCompliant(false)]
public static bool ToOrNull<T>(this IConvertible value, out T newObj, IFormatProvider provider = null)
where T : class
{
return ToOrOther(value, out newObj, null, provider);
}
}
}

View File

@ -22,8 +22,10 @@
/// </summary>
/// <param name="date">The date from which to start.</param>
/// <param name="asOf">The date at which to stop counting.</param>
/// <returns>Returns the integer number of years since <paramref name="date"/> as of
/// <paramref name="asOf"/>.</returns>
/// <returns>
/// Returns the integer number of years since <paramref name="date" /> as of
/// <paramref name="asOf" />.
/// </returns>
public static int Age(this DateTime date, DateTime asOf)
{
return (int)(((asOf.Date - TimeSpan.FromDays(1) - date.Date).TotalDays + 1) / 365.2425);
@ -37,7 +39,7 @@
/// <returns>Returns a date representing the first occurence of <paramref name="dayOfWeek" />.</returns>
public static DateTime First(this DateTime current, DayOfWeek dayOfWeek)
{
DateTime first = current.FirstDayOfMonth();
var first = current.FirstDayOfMonth();
if (first.DayOfWeek != dayOfWeek)
{
@ -57,6 +59,23 @@
return current.AddDays(1 - current.Day);
}
/// <summary>
/// Gets a <see cref="DateTime" /> representing the last specified day in the current month.
/// </summary>
/// <param name="current">The current date.</param>
/// <param name="dayOfWeek">The current day of week.</param>
/// <returns>Returns a date representing the final occurence of <paramref name="dayOfWeek" />.</returns>
public static DateTime Last(this DateTime current, DayOfWeek dayOfWeek)
{
var last = current.LastDayOfMonth();
var lastDayOfWeek = last.DayOfWeek;
var diff = dayOfWeek - lastDayOfWeek;
var offset = diff > 0 ? diff - 7 : diff;
return last.AddDays(offset);
}
/// <summary>
/// Gets a <see cref="DateTime" /> representing the last day in the current month.
/// </summary>
@ -68,23 +87,6 @@
return new DateTime(current.Year, current.Month, daysInMonth);
}
/// <summary>
/// Gets a <see cref="DateTime"/> representing the last specified day in the current month.
/// </summary>
/// <param name="current">The current date.</param>
/// <param name="dayOfWeek">The current day of week.</param>
/// <returns>Returns a date representing the final occurence of <paramref name="dayOfWeek"/>.</returns>
public static DateTime Last(this DateTime current, DayOfWeek dayOfWeek)
{
DateTime last = current.LastDayOfMonth();
DayOfWeek lastDayOfWeek = last.DayOfWeek;
var diff = dayOfWeek - lastDayOfWeek;
var offset = diff > 0 ? diff - 7 : diff;
return last.AddDays(offset);
}
/// <summary>
/// Gets a <see cref="DateTime" /> representing the first date following the current date which falls on the
/// given day of the week.
@ -108,8 +110,10 @@
/// Converts the <see cref="DateTime" /> to a Unix timestamp.
/// </summary>
/// <param name="time">The <see cref="DateTime" /> instance.</param>
/// <param name="useMillis">Optional. Whether or not the return value should be represented as milliseconds.
/// Defaults to <see langword="false"/>.</param>
/// <param name="useMillis">
/// Optional. Whether or not the return value should be represented as milliseconds.
/// Defaults to <see langword="false" />.
/// </param>
/// <returns>Returns a Unix timestamp representing the provided <see cref="DateTime" />.</returns>
public static long ToUnixTimeStamp(this DateTime time, bool useMillis = false)
{

View File

@ -27,7 +27,7 @@
: value.ToString();
}
IEnumerable<string> strings = dictionary.Select(o => $"{o.Key}={SanitizeValue(o.Value)}");
var strings = dictionary.Select(o => $"{o.Key}={SanitizeValue(o.Value)}");
return string.Join(";", strings);
}

View File

@ -13,9 +13,11 @@
/// <param name="value">The value to clamp.</param>
/// <param name="min">The minimum value.</param>
/// <param name="max">The maximum value.</param>
/// <returns>Returns <paramref name="max"/> if <paramref name="value"/> is greater than it,
/// <returns>
/// Returns <paramref name="max" /> if <paramref name="value" /> is greater than it,
/// <paramref name="min" /> if <paramref name="value" /> is less than it,
/// or <paramref name="value"/> itself otherwise.</returns>
/// or <paramref name="value" /> itself otherwise.
/// </returns>
public static double Clamp(this double value, double min, double max)
{
return Math.Min(Math.Max(value, min), max);
@ -28,7 +30,7 @@
/// <returns>Returns <paramref name="angle" /> in radians.</returns>
public static double DegreesToRadians(this double angle)
{
return Math.PI * angle / 180.0;
return (Math.PI * angle) / 180.0;
}
/// <summary>
@ -45,8 +47,10 @@
/// Determines if the <see cref="double" /> is even.
/// </summary>
/// <param name="number">The number.</param>
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is even, <see langword="false"/>
/// otherwise.</returns>
/// <returns>
/// Returns <see langword="true" /> if <paramref name="number" /> is even, <see langword="false" />
/// otherwise.
/// </returns>
public static bool IsEven(this double number)
{
return Math.Abs(number % 2.0) < double.Epsilon;
@ -56,8 +60,10 @@
/// Determines if the <see cref="double" /> is odd.
/// </summary>
/// <param name="number">The number.</param>
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is odd, <see langword="false"/>
/// otherwise.</returns>
/// <returns>
/// Returns <see langword="true" /> if <paramref name="number" /> is odd, <see langword="false" />
/// otherwise.
/// </returns>
public static bool IsOdd(this double number)
{
return !number.IsEven();
@ -79,7 +85,7 @@
/// <param name="v">The value to round.</param>
/// <param name="nearest">The nearest value.</param>
/// <returns>Returns the rounded value.</returns>
public static double Round(this double v, int nearest = 1)
public static double Round(this double v, double nearest = 1)
{
return Math.Round(v / nearest) * nearest;
}

View File

@ -11,15 +11,17 @@
/// Gets the endpoint hostname.
/// </summary>
/// <param name="endPoint">The endpoint whose hostname to get.</param>
/// <returns>Returns a <see cref="string"/> representing the hostname, which may be an IP or a DNS, or empty
/// string on failure.</returns>
/// <returns>
/// Returns a <see cref="string" /> representing the hostname, which may be an IP or a DNS, or empty
/// string on failure.
/// </returns>
public static string GetHost(this EndPoint endPoint)
{
return endPoint switch
{
IPEndPoint ip => ip.Address.ToString(),
DnsEndPoint dns => dns.Host,
_ => string.Empty,
var _ => string.Empty,
};
}
@ -34,7 +36,7 @@
{
IPEndPoint ip => ip.Port,
DnsEndPoint dns => dns.Port,
_ => 0,
var _ => 0,
};
}
}

View File

@ -14,11 +14,13 @@
/// <typeparam name="T">Any type.</typeparam>
/// <param name="value">The collection to split.</param>
/// <param name="chunkSize">The maximum length of the nested <see cref="byte" /> collection.</param>
/// <returns>Returns an <see cref="IEnumerable{T}"/> of <see cref="IEnumerable{T}"/> of <see cref="byte"/>
/// values.</returns>
/// <returns>
/// Returns an <see cref="IEnumerable{T}" /> of <see cref="IEnumerable{T}" /> of <see cref="byte" />
/// values.
/// </returns>
public static IEnumerable<IEnumerable<T>> Split<T>(this IEnumerable<T> value, int chunkSize)
{
T[] enumerable = value.ToArray();
var enumerable = value.ToArray();
var count = enumerable.LongCount();
chunkSize = chunkSize.Clamp(1, enumerable.Length);

View File

@ -13,9 +13,11 @@
/// <param name="value">The value to clamp.</param>
/// <param name="min">The minimum value.</param>
/// <param name="max">The maximum value.</param>
/// <returns>Returns <paramref name="max"/> if <paramref name="value"/> is greater than it,
/// <returns>
/// Returns <paramref name="max" /> if <paramref name="value" /> is greater than it,
/// <paramref name="min" /> if <paramref name="value" /> is less than it,
/// or <paramref name="value"/> itself otherwise.</returns>
/// or <paramref name="value" /> itself otherwise.
/// </returns>
public static short Clamp(this short value, short min, short max)
{
return Math.Min(Math.Max(value, min), max);
@ -27,9 +29,11 @@
/// <param name="value">The value to clamp.</param>
/// <param name="min">The minimum value.</param>
/// <param name="max">The maximum value.</param>
/// <returns>Returns <paramref name="max"/> if <paramref name="value"/> is greater than it,
/// <returns>
/// Returns <paramref name="max" /> if <paramref name="value" /> is greater than it,
/// <paramref name="min" /> if <paramref name="value" /> is less than it,
/// or <paramref name="value"/> itself otherwise.</returns>
/// or <paramref name="value" /> itself otherwise.
/// </returns>
[CLSCompliant(false)]
public static ushort Clamp(this ushort value, ushort min, ushort max)
{
@ -40,10 +44,14 @@
/// Converts the <see cref="short" /> to a <see cref="DateTime" /> treating it as a Unix timestamp.
/// </summary>
/// <param name="timestamp">The timestamp.</param>
/// <param name="isMillis">Optional. Whether or not the input value should be treated as milliseconds. Defaults
/// to <see langword="false"/>..</param>
/// <returns>Returns a <see cref="DateTime"/> representing <paramref name="timestamp"/> seconds since the Unix
/// epoch.</returns>
/// <param name="isMillis">
/// Optional. Whether or not the input value should be treated as milliseconds. Defaults
/// to <see langword="false" />..
/// </param>
/// <returns>
/// Returns a <see cref="DateTime" /> representing <paramref name="timestamp" /> seconds since the Unix
/// epoch.
/// </returns>
public static DateTime FromUnixTimestamp(this short timestamp, bool isMillis = false)
{
return ((long)timestamp).FromUnixTimestamp(isMillis);
@ -74,8 +82,10 @@
/// Determines if the <see cref="short" /> is even.
/// </summary>
/// <param name="number">The number.</param>
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is even, <see langword="false"/>
/// otherwise.</returns>
/// <returns>
/// Returns <see langword="true" /> if <paramref name="number" /> is even, <see langword="false" />
/// otherwise.
/// </returns>
public static bool IsEven(this short number)
{
return ((long)number).IsEven();
@ -85,8 +95,10 @@
/// Determines if the <see cref="ushort" /> is even.
/// </summary>
/// <param name="number">The number.</param>
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is even, <see langword="false"/>
/// otherwise.</returns>
/// <returns>
/// Returns <see langword="true" /> if <paramref name="number" /> is even, <see langword="false" />
/// otherwise.
/// </returns>
[CLSCompliant(false)]
public static bool IsEven(this ushort number)
{
@ -97,8 +109,10 @@
/// Determines if the <see cref="short" /> is odd.
/// </summary>
/// <param name="number">The number.</param>
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is odd, <see langword="false"/>
/// otherwise.</returns>
/// <returns>
/// Returns <see langword="true" /> if <paramref name="number" /> is odd, <see langword="false" />
/// otherwise.
/// </returns>
public static bool IsOdd(this short number)
{
return !number.IsEven();
@ -108,8 +122,10 @@
/// Determines if the <see cref="ushort" /> is odd.
/// </summary>
/// <param name="number">The number.</param>
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is odd, <see langword="false"/>
/// otherwise.</returns>
/// <returns>
/// Returns <see langword="true" /> if <paramref name="number" /> is odd, <see langword="false" />
/// otherwise.
/// </returns>
[CLSCompliant(false)]
public static bool IsOdd(this ushort number)
{
@ -120,8 +136,10 @@
/// Determines if the <see cref="short" /> is a prime number.
/// </summary>
/// <param name="number">The number.</param>
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is prime, <see langword="false"/>
/// otherwise.</returns>
/// <returns>
/// Returns <see langword="true" /> if <paramref name="number" /> is prime, <see langword="false" />
/// otherwise.
/// </returns>
public static bool IsPrime(this short number)
{
return ((long)number).IsPrime();
@ -131,8 +149,10 @@
/// Determines if the <see cref="short" /> is a prime number.
/// </summary>
/// <param name="number">The number.</param>
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is prime, <see langword="false"/>
/// otherwise.</returns>
/// <returns>
/// Returns <see langword="true" /> if <paramref name="number" /> is prime, <see langword="false" />
/// otherwise.
/// </returns>
[CLSCompliant(false)]
public static bool IsPrime(this ushort number)
{
@ -143,8 +163,10 @@
/// Gets an boolean value that represents this integer.
/// </summary>
/// <param name="value">The integer.</param>
/// <returns>Returns <see langword="false"/> if <paramref name="value"/> is 0,
/// <see langword="true"/> otherwise.</returns>
/// <returns>
/// Returns <see langword="false" /> if <paramref name="value" /> is 0,
/// <see langword="true" /> otherwise.
/// </returns>
public static bool ToBoolean(this short value)
{
return ((long)value).ToBoolean();
@ -154,8 +176,10 @@
/// Gets an boolean value that represents this integer.
/// </summary>
/// <param name="value">The integer.</param>
/// <returns>Returns <see langword="false"/> if <paramref name="value"/> is 0,
/// <see langword="true"/> otherwise.</returns>
/// <returns>
/// Returns <see langword="false" /> if <paramref name="value" /> is 0,
/// <see langword="true" /> otherwise.
/// </returns>
[CLSCompliant(false)]
public static bool ToBoolean(this ushort value)
{

View File

@ -13,9 +13,11 @@
/// <param name="value">The value to clamp.</param>
/// <param name="min">The minimum value.</param>
/// <param name="max">The maximum value.</param>
/// <returns>Returns <paramref name="max"/> if <paramref name="value"/> is greater than it,
/// <returns>
/// Returns <paramref name="max" /> if <paramref name="value" /> is greater than it,
/// <paramref name="min" /> if <paramref name="value" /> is less than it,
/// or <paramref name="value"/> itself otherwise.</returns>
/// or <paramref name="value" /> itself otherwise.
/// </returns>
public static int Clamp(this int value, int min, int max)
{
return Math.Min(Math.Max(value, min), max);
@ -27,9 +29,11 @@
/// <param name="value">The value to clamp.</param>
/// <param name="min">The minimum value.</param>
/// <param name="max">The maximum value.</param>
/// <returns>Returns <paramref name="max"/> if <paramref name="value"/> is greater than it,
/// <returns>
/// Returns <paramref name="max" /> if <paramref name="value" /> is greater than it,
/// <paramref name="min" /> if <paramref name="value" /> is less than it,
/// or <paramref name="value"/> itself otherwise.</returns>
/// or <paramref name="value" /> itself otherwise.
/// </returns>
[CLSCompliant(false)]
public static uint Clamp(this uint value, uint min, uint max)
{
@ -40,10 +44,14 @@
/// Converts the <see cref="int" /> to a <see cref="DateTime" /> treating it as a Unix timestamp.
/// </summary>
/// <param name="timestamp">The timestamp.</param>
/// <param name="isMillis">Optional. Whether or not the input value should be treated as milliseconds. Defaults
/// to <see langword="false"/>..</param>
/// <returns>Returns a <see cref="DateTime"/> representing <paramref name="timestamp"/> seconds since the Unix
/// epoch.</returns>
/// <param name="isMillis">
/// Optional. Whether or not the input value should be treated as milliseconds. Defaults
/// to <see langword="false" />..
/// </param>
/// <returns>
/// Returns a <see cref="DateTime" /> representing <paramref name="timestamp" /> seconds since the Unix
/// epoch.
/// </returns>
public static DateTime FromUnixTimestamp(this int timestamp, bool isMillis = false)
{
return ((long)timestamp).FromUnixTimestamp(isMillis);
@ -74,8 +82,10 @@
/// Determines if the <see cref="int" /> is even.
/// </summary>
/// <param name="number">The number.</param>
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is even, <see langword="false"/>
/// otherwise.</returns>
/// <returns>
/// Returns <see langword="true" /> if <paramref name="number" /> is even, <see langword="false" />
/// otherwise.
/// </returns>
public static bool IsEven(this int number)
{
return ((long)number).IsEven();
@ -85,8 +95,10 @@
/// Determines if the <see cref="uint" /> is even.
/// </summary>
/// <param name="number">The number.</param>
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is even, <see langword="false"/>
/// otherwise.</returns>
/// <returns>
/// Returns <see langword="true" /> if <paramref name="number" /> is even, <see langword="false" />
/// otherwise.
/// </returns>
[CLSCompliant(false)]
public static bool IsEven(this uint number)
{
@ -97,8 +109,10 @@
/// Determines if the <see cref="int" /> is odd.
/// </summary>
/// <param name="number">The number.</param>
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is odd, <see langword="false"/>
/// otherwise.</returns>
/// <returns>
/// Returns <see langword="true" /> if <paramref name="number" /> is odd, <see langword="false" />
/// otherwise.
/// </returns>
public static bool IsOdd(this int number)
{
return !number.IsEven();
@ -108,8 +122,10 @@
/// Determines if the <see cref="uint" /> is odd.
/// </summary>
/// <param name="number">The number.</param>
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is odd, <see langword="false"/>
/// otherwise.</returns>
/// <returns>
/// Returns <see langword="true" /> if <paramref name="number" /> is odd, <see langword="false" />
/// otherwise.
/// </returns>
[CLSCompliant(false)]
public static bool IsOdd(this uint number)
{
@ -120,8 +136,10 @@
/// Determines if the <see cref="int" /> is a prime number.
/// </summary>
/// <param name="number">The number.</param>
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is prime, <see langword="false"/>
/// otherwise.</returns>
/// <returns>
/// Returns <see langword="true" /> if <paramref name="number" /> is prime, <see langword="false" />
/// otherwise.
/// </returns>
public static bool IsPrime(this int number)
{
return ((long)number).IsPrime();
@ -131,8 +149,10 @@
/// Gets an boolean value that represents this integer.
/// </summary>
/// <param name="value">The integer.</param>
/// <returns>Returns <see langword="false"/> if <paramref name="value"/> is 0,
/// <see langword="true"/> otherwise.</returns>
/// <returns>
/// Returns <see langword="false" /> if <paramref name="value" /> is 0,
/// <see langword="true" /> otherwise.
/// </returns>
public static bool ToBoolean(this int value)
{
return ((long)value).ToBoolean();
@ -142,8 +162,10 @@
/// Gets an boolean value that represents this integer.
/// </summary>
/// <param name="value">The integer.</param>
/// <returns>Returns <see langword="false"/> if <paramref name="value"/> is 0,
/// <see langword="true"/> otherwise.</returns>
/// <returns>
/// Returns <see langword="false" /> if <paramref name="value" /> is 0,
/// <see langword="true" /> otherwise.
/// </returns>
[CLSCompliant(false)]
public static bool ToBoolean(this uint value)
{

View File

@ -13,9 +13,11 @@
/// <param name="value">The value to clamp.</param>
/// <param name="min">The minimum value.</param>
/// <param name="max">The maximum value.</param>
/// <returns>Returns <paramref name="max"/> if <paramref name="value"/> is greater than it,
/// <returns>
/// Returns <paramref name="max" /> if <paramref name="value" /> is greater than it,
/// <paramref name="min" /> if <paramref name="value" /> is less than it,
/// or <paramref name="value"/> itself otherwise.</returns>
/// or <paramref name="value" /> itself otherwise.
/// </returns>
public static long Clamp(this long value, long min, long max)
{
return Math.Min(Math.Max(value, min), max);
@ -27,9 +29,11 @@
/// <param name="value">The value to clamp.</param>
/// <param name="min">The minimum value.</param>
/// <param name="max">The maximum value.</param>
/// <returns>Returns <paramref name="max"/> if <paramref name="value"/> is greater than it,
/// <returns>
/// Returns <paramref name="max" /> if <paramref name="value" /> is greater than it,
/// <paramref name="min" /> if <paramref name="value" /> is less than it,
/// or <paramref name="value"/> itself otherwise.</returns>
/// or <paramref name="value" /> itself otherwise.
/// </returns>
[CLSCompliant(false)]
public static ulong Clamp(this ulong value, ulong min, ulong max)
{
@ -40,13 +44,17 @@
/// Converts the <see cref="long" /> to a <see cref="DateTime" /> treating it as a Unix timestamp.
/// </summary>
/// <param name="timestamp">The timestamp.</param>
/// <param name="isMillis">Optional. Whether or not the input value should be treated as milliseconds. Defaults
/// to <see langword="false"/>.</param>
/// <returns>Returns a <see cref="DateTime"/> representing <paramref name="timestamp"/> seconds since the Unix
/// epoch.</returns>
/// <param name="isMillis">
/// Optional. Whether or not the input value should be treated as milliseconds. Defaults
/// to <see langword="false" />.
/// </param>
/// <returns>
/// Returns a <see cref="DateTime" /> representing <paramref name="timestamp" /> seconds since the Unix
/// epoch.
/// </returns>
public static DateTime FromUnixTimestamp(this long timestamp, bool isMillis = false)
{
DateTimeOffset offset = isMillis
var offset = isMillis
? DateTimeOffset.FromUnixTimeMilliseconds(timestamp)
: DateTimeOffset.FromUnixTimeSeconds(timestamp);
@ -78,8 +86,10 @@
/// Determines if the <see cref="long" /> is even.
/// </summary>
/// <param name="number">The number.</param>
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is even, <see langword="false"/>
/// otherwise.</returns>
/// <returns>
/// Returns <see langword="true" /> if <paramref name="number" /> is even, <see langword="false" />
/// otherwise.
/// </returns>
public static bool IsEven(this long number)
{
return Math.Abs(number % 2.0) < double.Epsilon;
@ -89,8 +99,10 @@
/// Determines if the <see cref="ulong" /> is even.
/// </summary>
/// <param name="number">The number.</param>
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is even, <see langword="false"/>
/// otherwise.</returns>
/// <returns>
/// Returns <see langword="true" /> if <paramref name="number" /> is even, <see langword="false" />
/// otherwise.
/// </returns>
[CLSCompliant(false)]
public static bool IsEven(this ulong number)
{
@ -101,8 +113,10 @@
/// Determines if the <see cref="long" /> is odd.
/// </summary>
/// <param name="number">The number.</param>
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is odd, <see langword="false"/>
/// otherwise.</returns>
/// <returns>
/// Returns <see langword="true" /> if <paramref name="number" /> is odd, <see langword="false" />
/// otherwise.
/// </returns>
public static bool IsOdd(this long number)
{
return !IsEven(number);
@ -112,8 +126,10 @@
/// Determines if the <see cref="ulong" /> is odd.
/// </summary>
/// <param name="number">The number.</param>
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is odd, <see langword="false"/>
/// otherwise.</returns>
/// <returns>
/// Returns <see langword="true" /> if <paramref name="number" /> is odd, <see langword="false" />
/// otherwise.
/// </returns>
[CLSCompliant(false)]
public static bool IsOdd(this ulong number)
{
@ -124,8 +140,10 @@
/// Determines if the <see cref="long" /> is a prime number.
/// </summary>
/// <param name="number">The number.</param>
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is prime, <see langword="false"/>
/// otherwise.</returns>
/// <returns>
/// Returns <see langword="true" /> if <paramref name="number" /> is prime, <see langword="false" />
/// otherwise.
/// </returns>
public static bool IsPrime(this long number)
{
if (number <= 1)
@ -159,8 +177,10 @@
/// Determines if the <see cref="ulong" /> is a prime number.
/// </summary>
/// <param name="number">The number.</param>
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is prime, <see langword="false"/>
/// otherwise.</returns>
/// <returns>
/// Returns <see langword="true" /> if <paramref name="number" /> is prime, <see langword="false" />
/// otherwise.
/// </returns>
[CLSCompliant(false)]
public static bool IsPrime(this ulong number)
{
@ -195,8 +215,10 @@
/// Gets an boolean value that represents this integer.
/// </summary>
/// <param name="value">The integer.</param>
/// <returns>Returns <see langword="false"/> if <paramref name="value"/> is 0,
/// <see langword="true"/> otherwise.</returns>
/// <returns>
/// Returns <see langword="false" /> if <paramref name="value" /> is 0,
/// <see langword="true" /> otherwise.
/// </returns>
public static bool ToBoolean(this long value)
{
return value != 0;
@ -206,8 +228,10 @@
/// Gets an boolean value that represents this integer.
/// </summary>
/// <param name="value">The integer.</param>
/// <returns>Returns <see langword="false"/> if <paramref name="value"/> is 0,
/// <see langword="true"/> otherwise.</returns>
/// <returns>
/// Returns <see langword="false" /> if <paramref name="value" /> is 0,
/// <see langword="true" /> otherwise.
/// </returns>
[CLSCompliant(false)]
public static bool ToBoolean(this ulong value)
{

View File

@ -20,8 +20,10 @@
/// generation.
/// </summary>
/// <param name="random">The <see cref="System.Random" /> instance.</param>
/// <returns>Returns <see langword="true"/> or <see langword="false"/> depending on the return value
/// from <see cref="System.Random.Next(int)"/>.</returns>
/// <returns>
/// Returns <see langword="true" /> or <see langword="false" /> depending on the return value
/// from <see cref="System.Random.Next(int)" />.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="random" /> is <see langword="null" />.</exception>
public static bool CoinToss(this Random random)
{
@ -52,8 +54,10 @@
/// <param name="random">The <see cref="System.Random" /> instance.</param>
/// <param name="source">The collection from which to draw.</param>
/// <returns>Returns a random element of type <see cref="T" /> from <paramref name="source" />.</returns>
/// <exception cref="ArgumentNullException"><paramref name="random"/> or <paramref name="source"/> is
/// <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException">
/// <paramref name="random" /> or <paramref name="source" /> is
/// <see langword="null" />.
/// </exception>
public static T OneOf<T>(this Random random, IList<T> source)
{
if (random is null)

View File

@ -14,8 +14,10 @@
/// <see langword="default" /> if none exists.
/// </summary>
/// <param name="member">The member.</param>
/// <returns>Returns an <see cref="object"/> representing the value stored in this member's
/// <see cref="DefaultValueAttribute"/>.</returns>
/// <returns>
/// Returns an <see cref="object" /> representing the value stored in this member's
/// <see cref="DefaultValueAttribute" />.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="member" /> is <see langword="null" />.</exception>
public static object GetDefaultValue(this MemberInfo member)
{
@ -38,8 +40,10 @@
/// </summary>
/// <typeparam name="T">The type to which the value should cast.</typeparam>
/// <param name="member">The member.</param>
/// <returns>Returns an instance of <typeparamref name="T"/> representing the value stored in this member's
/// <see cref="DefaultValueAttribute"/>.</returns>
/// <returns>
/// Returns an instance of <typeparamref name="T" /> representing the value stored in this member's
/// <see cref="DefaultValueAttribute" />.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="member" /> is <see langword="null" />.</exception>
public static T GetDefaultValue<T>(this MemberInfo member)
{
@ -56,8 +60,10 @@
/// <see langword="null" /> if none exists.
/// </summary>
/// <param name="member">The member.</param>
/// <returns>Returns an instance of <typeparamref name="T"/> representing the value stored in this member's
/// <see cref="DescriptionAttribute"/>.</returns>
/// <returns>
/// Returns a string representing the value stored in this member's
/// <see cref="DescriptionAttribute" />.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="member" /> is <see langword="null" />.</exception>
public static string GetDescription(this MemberInfo member)
{
@ -82,11 +88,15 @@
/// <typeparam name="TReturn">The return type of the <paramref name="selector" /> delegate.</typeparam>
/// <param name="member">The member.</param>
/// <param name="selector">The selector delegate.</param>
/// <returns>Returns an instance of <typeparamref name="TReturn"/> as provided from
/// <paramref name="selector"/>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="member"/> is <see langword="null"/>
/// <returns>
/// Returns an instance of <typeparamref name="TReturn" /> as provided from
/// <paramref name="selector" />.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="member" /> is <see langword="null" />
/// -or-
/// <paramref name="selector"/> is <see langword="null"/>.</exception>
/// <paramref name="selector" /> is <see langword="null" />.
/// </exception>
public static TReturn SelectFromCustomAttribute<TAttribute, TReturn>(
this MemberInfo member,
Func<TAttribute, TReturn> selector)

View File

@ -13,9 +13,11 @@
/// <param name="value">The value to clamp.</param>
/// <param name="min">The minimum value.</param>
/// <param name="max">The maximum value.</param>
/// <returns>Returns <paramref name="max"/> if <paramref name="value"/> is greater than it,
/// <returns>
/// Returns <paramref name="max" /> if <paramref name="value" /> is greater than it,
/// <paramref name="min" /> if <paramref name="value" /> is less than it,
/// or <paramref name="value"/> itself otherwise.</returns>
/// or <paramref name="value" /> itself otherwise.
/// </returns>
public static float Clamp(this float value, float min, float max)
{
return Math.Min(Math.Max(value, min), max);
@ -45,8 +47,10 @@
/// Determines if the <see cref="float" /> is even.
/// </summary>
/// <param name="number">The number.</param>
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is even, <see langword="false"/>
/// otherwise.</returns>
/// <returns>
/// Returns <see langword="true" /> if <paramref name="number" /> is even, <see langword="false" />
/// otherwise.
/// </returns>
public static bool IsEven(this float number)
{
return ((double)number).IsEven();
@ -56,8 +60,10 @@
/// Determines if the <see cref="float" /> is odd.
/// </summary>
/// <param name="number">The number.</param>
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is odd, <see langword="false"/>
/// otherwise.</returns>
/// <returns>
/// Returns <see langword="true" /> if <paramref name="number" /> is odd, <see langword="false" />
/// otherwise.
/// </returns>
public static bool IsOdd(this float number)
{
return !number.IsEven();
@ -79,7 +85,7 @@
/// <param name="v">The value to round.</param>
/// <param name="nearest">The nearest value.</param>
/// <returns>Returns the rounded value.</returns>
public static float Round(this float v, int nearest = 1)
public static float Round(this float v, float nearest = 1)
{
return (float)((double)v).Round(nearest);
}

View File

@ -2,7 +2,6 @@
{
using System;
using System.IO;
using System.Reflection;
using System.Security.Cryptography;
/// <summary>
@ -25,7 +24,7 @@
throw new ArgumentNullException(nameof(stream));
}
MethodInfo create = typeof(T).GetMethod("Create", Array.Empty<Type>());
var create = typeof(T).GetMethod("Create", Array.Empty<Type>());
using var crypt = (T)create?.Invoke(null, null);
return crypt?.ComputeHash(stream);
}

View File

@ -38,13 +38,17 @@
/// <param name="str">The input string.</param>
/// <param name="from">The input encoding.</param>
/// <param name="to">The output encoding.</param>
/// <returns>Returns a new <see cref="string"/> with its data converted to
/// <paramref name="to"/>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="str"/> is <see langword="null"/>
/// <returns>
/// Returns a new <see cref="string" /> with its data converted to
/// <paramref name="to" />.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="str" /> is <see langword="null" />
/// - or -
/// <paramref name="from" /> is <see langword="null" />
/// -or
/// <paramref name="to"/> is <see langword="null"/>.</exception>
/// <paramref name="to" /> is <see langword="null" />.
/// </exception>
public static string ChangeEncoding(this string str, Encoding from, Encoding to)
{
if (str is null)
@ -71,8 +75,10 @@
/// <typeparam name="T">The type of the <see cref="Enum" />.</typeparam>
/// <param name="value">The <see cref="string" /> value to parse.</param>
/// <returns>The <see cref="Enum" /> value corresponding to the <see cref="string" />.</returns>
/// <remarks>Credit for this method goes to Scott Dorman:
/// (http://geekswithblogs.net/sdorman/Default.aspx)</remarks>
/// <remarks>
/// Credit for this method goes to Scott Dorman:
/// (http://geekswithblogs.net/sdorman/Default.aspx).
/// </remarks>
public static T EnumParse<T>(this string value)
{
return value.EnumParse<T>(false);
@ -85,8 +91,10 @@
/// <param name="value">The <see cref="string" /> value to parse.</param>
/// <param name="ignoreCase">Whether or not to ignore casing.</param>
/// <returns>The <see cref="Enum" /> value corresponding to the <see cref="string" />.</returns>
/// <remarks>Credit for this method goes to Scott Dorman:
/// (http://geekswithblogs.net/sdorman/Default.aspx)</remarks>
/// <remarks>
/// Credit for this method goes to Scott Dorman:
/// (http://geekswithblogs.net/sdorman/Default.aspx).
/// </remarks>
public static T EnumParse<T>(this string value, bool ignoreCase)
{
if (value is null)
@ -101,7 +109,7 @@
throw new ArgumentException(Resource.EnumParseEmptyStringException, nameof(value));
}
Type t = typeof(T);
var t = typeof(T);
if (!t.IsEnum)
{
@ -128,8 +136,10 @@
/// <param name="str">The string to convert.</param>
/// <param name="encoding">The encoding to use.</param>
/// <returns>Returns a <see cref="byte" />[].</returns>
/// <exception cref="ArgumentNullException"><paramref name="str"/> or <paramref name="encoding"/> or both are
/// <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException">
/// <paramref name="str" /> or <paramref name="encoding" /> or both are
/// <see langword="null" />.
/// </exception>
public static byte[] GetBytes(this string str, Encoding encoding)
{
if (str is null)
@ -149,8 +159,10 @@
/// Determines if all alpha characters in this string are considered lowercase.
/// </summary>
/// <param name="str">The input string.</param>
/// <returns>Returns <see langword="true"/> if all alpha characters are lowercase, <see langword="false"/>
/// otherwise.</returns>
/// <returns>
/// Returns <see langword="true" /> if all alpha characters are lowercase, <see langword="false" />
/// otherwise.
/// </returns>
public static bool IsLower(this string str)
{
return str.Where(char.IsLetter).All(char.IsLower);
@ -160,8 +172,10 @@
/// Determines if all alpha characters in this string are considered uppercase.
/// </summary>
/// <param name="str">The input string.</param>
/// <returns>Returns <see langword="true"/> if all alpha characters are uppercase, <see langword="false"/>
/// otherwise.</returns>
/// <returns>
/// Returns <see langword="true" /> if all alpha characters are uppercase, <see langword="false" />
/// otherwise.
/// </returns>
public static bool IsUpper(this string str)
{
return str.Where(char.IsLetter).All(char.IsUpper);
@ -201,8 +215,10 @@
/// </summary>
/// <param name="str">The string to repeat.</param>
/// <param name="count">The repeat count.</param>
/// <returns>Returns a <see cref="string"/> whose value is <paramref name="str"/> repeated
/// <paramref name="count"/> times.</returns>
/// <returns>
/// Returns a <see cref="string" /> whose value is <paramref name="str" /> repeated
/// <paramref name="count" /> times.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="str" /> is <see langword="null" />.</exception>
public static string Repeat(this string str, int count)
{
@ -253,8 +269,10 @@
/// </summary>
/// <param name="str">The string to split.</param>
/// <param name="chunkSize">The maximum length of each string in the returned result.</param>
/// <returns>Returns an <see cref="IEnumerable{T}"/> containing <see cref="string"/> instances which are no
/// greater than <paramref name="chunkSize"/> in length.</returns>
/// <returns>
/// Returns an <see cref="IEnumerable{T}" /> containing <see cref="string" /> instances which are no
/// greater than <paramref name="chunkSize" /> in length.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="str" /> is <see langword="null" />.</exception>
public static IEnumerable<string> Split(this string str, int chunkSize)
{

View File

@ -12,8 +12,10 @@
/// </summary>
/// <typeparam name="T">An <see cref="Enum" />.</typeparam>
/// <param name="src">An <see cref="Enum" /> value.</param>
/// <param name="wrap">Optional. Whether or not to wrap to the to the start of the enum. Defaults to
/// true.</param>
/// <param name="wrap">
/// Optional. Whether or not to wrap to the to the start of the enum. Defaults to
/// true.
/// </param>
/// <returns>Returns a <see cref="T" /> value.</returns>
public static T Next<T>(this T src, bool wrap = true)
where T : struct
@ -33,8 +35,10 @@
/// </summary>
/// <typeparam name="T">An <see cref="Enum" />.</typeparam>
/// <param name="src">An <see cref="Enum" /> value.</param>
/// <param name="wrap">Optional. Whether or not to wrap to the to the end of the enum. Defaults to
/// true.</param>
/// <param name="wrap">
/// Optional. Whether or not to wrap to the to the end of the enum. Defaults to
/// true.
/// </param>
/// <returns>Returns a <see cref="T" /> value.</returns>
public static T Previous<T>(this T src, bool wrap = true)
where T : struct

View File

@ -26,7 +26,7 @@
$"(?:{realNumberPattern} *s)? *" +
$"(?:{realNumberPattern} *ms)?$";
Match match = Regex.Match(input, pattern);
var match = Regex.Match(input, pattern);
double weeks = 0, days = 0, hours = 0, minutes = 0, seconds = 0, milliseconds = 0;
if (match.Groups[1].Success)
@ -62,7 +62,7 @@
Trace.WriteLine($"Input: {input}");
Trace.WriteLine($"Parsed: {weeks}w {days}d {hours}h {minutes}m {seconds}s {milliseconds}ms");
TimeSpan span = TimeSpan.Zero;
var span = TimeSpan.Zero;
span += TimeSpan.FromDays(weeks * 7);
span += TimeSpan.FromDays(days);