A set of extensions to make working with some of the base class libraries easier.
using NetCoreExtensions.Strings
value.IsNullOrEmpty()
value.IsNotNullOrEmpty()
value.IsNullOrWhitespace()
value.IsNotNullOrWhitespace()
values.Join(separator)
value.EmptyToNull()
value.WhiteSpaceToNull()
Example: (" ".WhiteSpaceToNull() ?? "default") == "default"
using NetCoreExtensions.Regex
value.Match(pattern)
value.Match(pattern, options)
var name = "My Name";
if (name.Match("^my", RegexOptions.IgnoreCase)) {
// ...
}
if (value.TryMatch(pattern, out var match)) { .. }
if (value.TryMatch(pattern, options, out var match)) { .. }
var name = "My name is foo";
if (name.Match("my name is (?<name>[A-Za-z ]+)", out var match)) {
Console.WriteLine(match.Groups["name"].Value); // displays "foo"
}
When using Matches
, returns a IEnumerable<Match>
instead of MatchCollection
,
which means there is no need to do an explicit cast to Match
.
foreach (var match in value.Matches(pattern)) {
Console.WriteLine(match.Value);
}
Wrap async methods or tasks that don't provide timeout or cancelation support.
try {
var result1 = await SomethingAsync().TimeoutAfter(100);
var result2 = await SomethingElseAsync().TimeoutAfter(1200, cancellationToken);
} catch (TaskCancelationException) {
Console.WriteLine("Execution was canceled");
} catch (TaskTimeoutException ex) {
Console.WriteLine($"Task timed out after {ex.Timeout}");
}
using NetCoreExtensions.Security
Sha1()
,Sha256()
,Sha384()
,Sha512()
,HmacSha1()
,HmacSha256()
,HmacSha384()
,HmacSha512()
methods forstring
andbyte[]
- Return unix-style checksums, eg:
"b444ac06613fc8d63795be9ad0beaf55011936ac"
- Defaults to
Encoding.UTF8
forstring
inputs but can be overridden
using NetCoreExtensions.DateTime
These work with int
, long
or double
.
42.Milliseconds()
42.Seconds()
42.Minutes()
42.Hours()
42.Days()
42.Ticks()
using NetCoreExtensions.Enum
MyEnum.Val1.GetName()
42.ToEnum<MyEnum>()
returnsNullable<MyEnum>(null)
if invalid42.ToEnum(defaultValue)
returnsdefaultValue
if invalid"Val1".ToEnum<MyEnum>()
returnsNullable<MyEnum>
if invalid"Val1".ToEnum(defaultValue)
returnsdefaultValue
if invalid
This project is licensed under the MIT License.