Konvenience 1.0.1
dotnet add package Konvenience --version 1.0.1
NuGet\Install-Package Konvenience -Version 1.0.1
<PackageReference Include="Konvenience" Version="1.0.1" />
paket add Konvenience --version 1.0.1
#r "nuget: Konvenience, 1.0.1"
// Install Konvenience as a Cake Addin
#addin nuget:?package=Konvenience&version=1.0.1
// Install Konvenience as a Cake Tool
#tool nuget:?package=Konvenience&version=1.0.1
Konvenience
This is a small library of extension convenience extension methods inspired on the ones present in the Kotlin standard library.
How to use it
Add the namespace to your project:
using Konvenience;
That's it, now you can use the available extension methods.
Available extension methods
Common
These methods are available to all objects.
T Also<T>(this T, Action<T>)
Executes the given action with the input object and returns the object itself.
Example use case:
// Initialize complex objects
val myObject = new MyClass().Also(o =>
{
o.Setup();
o.Configure();
});
TResult Let<T, TResult>(this T, Func<T, TResult> function)
Executes the given function with the input object and returns its result.
Example use case:
// Transform a value that shouldn't be null and get a default in case of null
var result = myObject?.let(transform) ?? DEFAULT_VALUE;
T Take(Reference|Value)(If|Unless)<T>(this T, Predicate<T>)
Set of methods that return the input object depending on the result of the input predicate, or null otherwise.
Given how class
and struct
handle null
differently, there are two versions of these extension methods: for references (TakeReferenceIf
, TakeReferenceUnless
) and for values (TakeValueIf
, TakeReferenceUnless
).
Example use case:
// Get a default value if the input object is not valid
var validString = myString.takeIf(s => s.Length > 10) ?? DEFAULT_STRING;
T As<T>(this object obj)
Executes a "safe cast" (using the as
operator) in the input object, this is useful to chain other extension methods. This method is only available to references (objects of type class
).
Example use case:
var result = input.As<OtherClass>().Let(o => o.Property);
Enumerables
These are several methods to perform operations on IEnumerable
objects.
void ForEach(...)
Executes the input Action<T>
in the input IEnumerable
.
Example use case:
void SomeFunction(int number) { ... }
// Somewhere else
myNumbers.ForEach(SomeFunction);
There are variants that accept an Action<T, int>
object where the second argument is the index and variants for dictionaries that accept Action<TKey, TValue>
objects.
T Get(...)
and T GetOrElse(...)
Given C# enumerable interfaces doesn't have a "get" operator, these can be used so chaining calls is easier:
var value = myNumbers
.Where(i => i < 10)
.TakeIf(numbers => numbers.Length > 10)
?.GetOrElse(0, NOT_FOUND)
?? RESPONSE_TOO_LONG;
bool IsEmpty(...)
and bool IsNotEmpty(...)
Return if the enumerable is (not) empty:
if (myNumbers.isNotEmpty()) {
return myNumbers[0];
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET Framework | net461 is compatible. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
This package has no dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|