Skip to content

Commit

Permalink
Added various "Get{Type}OrNull" extension methods for the "NameValueC…
Browse files Browse the repository at this point in the history
…ollection" class
  • Loading branch information
abjerner committed Nov 20, 2023
1 parent 20f32c4 commit a93bfd0
Show file tree
Hide file tree
Showing 2 changed files with 854 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ public static bool GetBoolean(this NameValueCollection? collection, string key,
return StringUtils.ParseBoolean(collection?.GetString(key), fallback);
}

/// <summary>
/// Returns the boolean value of the item with <paramref name="key"/>. If an matching isn't found, or conversion to a boolean representation failed, <see langword="null"/> is returned instead.
/// </summary>
/// <param name="collection">The collection.</param>
/// <param name="key">The key of the item.</param>
/// <returns>The converted value of the item with <paramref name="key"/> if the conversion was successful; otherwise <see langword="null"/>.</returns>
public static bool? GetBooleanOrNull(this NameValueCollection? collection, string key) {
return StringUtils.ParseBooleanOrNull(collection?.GetString(key));
}

/// <summary>
/// Gets the boolean value associated with the specified <paramref name="key"/>.
/// </summary>
Expand Down Expand Up @@ -114,6 +124,16 @@ public static Guid GetGuid(this NameValueCollection? collection, string key, Gui
return StringUtils.ParseGuid(collection?.GetString(key), fallback);
}

/// <summary>
/// Returns the GUID value of the item with <paramref name="key"/>. If an matching isn't found, or conversion to a GUID failed, <see langword="null"/> is returned instead.
/// </summary>
/// <param name="collection">The collection.</param>
/// <param name="key">The key of the item.</param>
/// <returns>The converted value of the item with <paramref name="key"/> if the conversion was successful; otherwise <c>false</c>.</returns>
public static Guid? GetGuidOrNull(this NameValueCollection? collection, string key) {
return StringUtils.ParseGuidOrNull(collection?.GetString(key));
}

/// <summary>
/// Gets the boolean value associated with the specified <paramref name="key"/>.
/// </summary>
Expand Down Expand Up @@ -161,6 +181,16 @@ public static int GetInt32(this NameValueCollection? collection, string key, int
return StringUtils.ParseInt32(collection?.GetString(key), fallback);
}

/// <summary>
/// Returns the <see cref="int"/> value of the item with the specified <paramref name="key"/>, or <see langword="null"/> if a matching item couldn't be found or the value couldn't be parsed to a 32-bit unsigned integer.
/// </summary>
/// <param name="collection">The collection.</param>
/// <param name="key">The key of the item.</param>
/// <returns>The value as a 32-bit unsigned integer (<see cref="int"/>).</returns>
public static int? GetInt32OrNull(this NameValueCollection? collection, string key) {
return StringUtils.ParseInt32OrNull(collection?.GetString(key));
}

/// <summary>
/// Gets the <see cref="int"/> value associated with the specified <paramref name="key"/>.
/// </summary>
Expand Down Expand Up @@ -208,6 +238,16 @@ public static long GetInt64(this NameValueCollection? collection, string key, lo
return StringUtils.ParseInt64(collection?.GetString(key), fallback);
}

/// <summary>
/// Returns the <see cref="long"/> value of the item with the specified <paramref name="key"/>, or <see langword="null"/> if a matching item couldn't be found or the value couldn't be parsed to a 64-bit unsigned integer.
/// </summary>
/// <param name="collection">The collection.</param>
/// <param name="key">The key of the item.</param>
/// <returns>The value as a 64-bit unsigned integer (<see cref="long"/>).</returns>
public static long? GetInt64OrNull(this NameValueCollection? collection, string key) {
return StringUtils.ParseInt64OrNull(collection?.GetString(key));
}

/// <summary>
/// Gets the <see cref="long"/> value associated with the specified <paramref name="key"/>.
/// </summary>
Expand Down Expand Up @@ -255,6 +295,16 @@ public static float GetFloat(this NameValueCollection? collection, string key, i
return StringUtils.ParseFloat(collection?.GetString(key), fallback);
}

/// <summary>
/// Returns the <see cref="float"/> value of the item with the specified <paramref name="key"/>, or <see langword="null"/> if a matching item couldn't be found or the value couldn't be parsed to a single-precision floating-point number.
/// </summary>
/// <param name="collection">The collection.</param>
/// <param name="key">The key of the item.</param>
/// <returns>The value as a single-precision floating-point number (<see cref="float"/>).</returns>
public static float? GetFloatOrNull(this NameValueCollection? collection, string key) {
return StringUtils.ParseFloatOrNull(collection?.GetString(key));
}

/// <summary>
/// Gets the <see cref="float"/> value associated with the specified <paramref name="key"/>.
/// </summary>
Expand Down Expand Up @@ -282,7 +332,7 @@ public static bool TryGetFloat(this NameValueCollection? collection, string key,
#region Double

/// <summary>
/// Returns the <see cref="long"/> value of the item with the specified <paramref name="key"/>, or <c>0</c> if a matching item couldn't be found or the value couldn't be parsed to a double-precision floating-point number.
/// Returns the <see cref="double"/> value of the item with the specified <paramref name="key"/>, or <c>0</c> if a matching item couldn't be found or the value couldn't be parsed to a double-precision floating-point number.
/// </summary>
/// <param name="collection">The collection.</param>
/// <param name="key">The key of the item.</param>
Expand All @@ -303,7 +353,17 @@ public static double GetDouble(this NameValueCollection? collection, string key,
}

/// <summary>
/// Gets the <see cref="long"/> value associated with the specified <paramref name="key"/>.
/// Returns the <see cref="double"/> value of the item with the specified <paramref name="key"/>, or <see langword="null"/> if a matching item couldn't be found or the value couldn't be parsed to a double-precision floating-point number.
/// </summary>
/// <param name="collection">The collection.</param>
/// <param name="key">The key of the item.</param>
/// <returns>The value as a double-precision floating-point number (<see cref="double"/>).</returns>
public static double? GetDoubleOrNull(this NameValueCollection? collection, string key) {
return StringUtils.ParseDoubleOrNull(collection?.GetString(key));
}

/// <summary>
/// Gets the <see cref="double"/> value associated with the specified <paramref name="key"/>.
/// </summary>
/// <param name="collection">The collection.</param>
/// <param name="key">The key of the item.</param>
Expand All @@ -314,7 +374,7 @@ public static bool TryGetDouble(this NameValueCollection? collection, string key
}

/// <summary>
/// Gets the <see cref="long"/> value associated with the specified <paramref name="key"/>.
/// Gets the <see cref="double"/> value associated with the specified <paramref name="key"/>.
/// </summary>
/// <param name="collection">The collection.</param>
/// <param name="key">The key of the item.</param>
Expand Down
Loading

0 comments on commit a93bfd0

Please sign in to comment.