ktsu.DelegateTransform
1.1.3
Prefix Reserved
dotnet add package ktsu.DelegateTransform --version 1.1.3
NuGet\Install-Package ktsu.DelegateTransform -Version 1.1.3
<PackageReference Include="ktsu.DelegateTransform" Version="1.1.3" />
<PackageVersion Include="ktsu.DelegateTransform" Version="1.1.3" />
<PackageReference Include="ktsu.DelegateTransform" />
paket add ktsu.DelegateTransform --version 1.1.3
#r "nuget: ktsu.DelegateTransform, 1.1.3"
#addin nuget:?package=ktsu.DelegateTransform&version=1.1.3
#tool nuget:?package=ktsu.DelegateTransform&version=1.1.3
ktsu.DelegateTransform
A utility library for transforming values using delegates in C#.
Introduction
DelegateTransform is a utility library that provides methods for transforming values using various delegate types in C#. It offers a clean and efficient way to apply transformations using ActionRef
, Func
, and FuncRef
delegates, making your code more readable and functional.
Features
- ActionRef Transformations: Modify values by reference using action delegates
- Func Transformations: Transform values using function delegates
- FuncRef Transformations: Transform values by reference using function delegates
- Fluent API: Chain multiple transformations together
- Generic Implementation: Works with any value or reference type
Installation
Package Manager Console
Install-Package ktsu.DelegateTransform
.NET CLI
dotnet add package ktsu.DelegateTransform
Package Reference
<PackageReference Include="ktsu.DelegateTransform" Version="x.y.z" />
Usage Examples
With ActionRef
The With
method can be used with an ActionRef
delegate to modify the input value by reference:
using ktsu.DelegateTransform;
int input = 5;
DelegateTransform.With(input, (ref int x) => x *= 2);
// input is now 10
With Func
The With
method can be used with a Func
delegate to transform the input value:
using ktsu.DelegateTransform;
int input = 5;
int result = DelegateTransform.With(input, (int x) => x * 2);
// result is 10
With FuncRef
The With
method can be used with a FuncRef
delegate to transform the input value by reference:
using ktsu.DelegateTransform;
int input = 5;
DelegateTransform.With(input, (ref int x) => x *= 2);
// input is now 10
Chaining Transformations
You can chain multiple transformations for more complex operations:
using ktsu.DelegateTransform;
// Starting value
string input = "example";
// Chain of transformations
var result = DelegateTransform
.With(input, s => s.ToUpper()) // EXAMPLE
.With(s => s.Replace("EX", "**")) // **AMPLE
.With(s => s + " transformed"); // **AMPLE transformed
Console.WriteLine(result); // Outputs: **AMPLE transformed
Complex Object Transformations
using ktsu.DelegateTransform;
var person = new Person { Name = "John", Age = 30 };
// Transform using ActionRef
DelegateTransform.With(person, (ref Person p) => {
p.Name = p.Name.ToUpper();
p.Age += 1;
});
Console.WriteLine($"{person.Name}, {person.Age}"); // Outputs: JOHN, 31
// Transform using Func
var description = DelegateTransform.With(person, p =>
$"{p.Name} is {p.Age} years old");
Console.WriteLine(description); // Outputs: JOHN is 31 years old
API Reference
DelegateTransform
Static Class
The main class providing transformation methods.
Methods
Name | Parameters | Return Type | Description |
---|---|---|---|
With<T> |
T value, Action<ref T> transform |
T |
Transforms a value by reference using an action delegate |
With<T, TResult> |
T value, Func<T, TResult> transform |
TResult |
Transforms a value using a function delegate |
With<T> |
T value, Func<ref T, T> transform |
T |
Transforms a value by reference using a function delegate |
With<T> |
ref T value, Action<ref T> transform |
void |
Transforms a reference to a value using an action delegate |
Advanced Usage
Performance Considerations
Using reference-based transformations (ActionRef
, FuncRef
) can be more performant for large structs as they avoid unnecessary copying:
// For large structs, using ref can be more efficient
var largeStruct = new LargeStruct(1000000);
// This avoids copying the struct
DelegateTransform.With(ref largeStruct, (ref LargeStruct s) => {
s.Process();
});
Custom Delegate Types
You can easily extend DelegateTransform with your own delegate types:
// Define a custom delegate type
public delegate void MyCustomDelegate<T>(ref T value, string parameter);
// Extension method for DelegateTransform
public static class DelegateTransformExtensions
{
public static T WithCustom<T>(this T value, MyCustomDelegate<T> transform, string parameter)
{
T result = value;
transform(ref result, parameter);
return result;
}
}
// Usage
var result = 5.WithCustom((ref int x, string p) => {
x *= int.Parse(p);
}, "3");
// result is 15
Contributing
Contributions are welcome! Here's how you can help:
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
Please make sure to update tests as appropriate and adhere to the existing coding style.
License
This project is licensed under the MIT License - see the LICENSE.md file for details.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- ktsu.ScopedAction (>= 1.1.2)
-
net9.0
- ktsu.ScopedAction (>= 1.1.2)
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 |
---|---|---|
1.1.3 | 137 | 5/21/2025 |
1.1.3-pre.17 | 116 | 5/20/2025 |
1.1.3-pre.15 | 76 | 5/17/2025 |
1.1.3-pre.14 | 123 | 5/16/2025 |
1.1.3-pre.13 | 197 | 5/15/2025 |
1.1.3-pre.12 | 199 | 5/14/2025 |
1.1.3-pre.11 | 199 | 5/13/2025 |
1.1.3-pre.10 | 231 | 5/12/2025 |
1.1.3-pre.9 | 157 | 5/11/2025 |
1.1.3-pre.8 | 105 | 5/10/2025 |
1.1.3-pre.7 | 45 | 5/9/2025 |
1.1.3-pre.6 | 116 | 5/8/2025 |
1.1.3-pre.5 | 116 | 5/7/2025 |
1.1.3-pre.4 | 112 | 5/6/2025 |
1.1.3-pre.3 | 119 | 5/5/2025 |
1.1.3-pre.2 | 120 | 5/4/2025 |
1.1.3-pre.1 | 116 | 5/4/2025 |
1.1.2 | 102 | 5/4/2025 |
1.1.2-pre.2 | 56 | 4/26/2025 |
1.1.2-pre.1 | 110 | 4/4/2025 |
1.1.1 | 159 | 3/30/2025 |
1.1.0 | 142 | 3/30/2025 |
1.0.2-pre.3 | 70 | 3/29/2025 |
1.0.2-pre.2 | 450 | 3/25/2025 |
1.0.2-pre.1 | 72 | 2/18/2025 |
1.0.1 | 111 | 2/18/2025 |
1.0.1-pre.16 | 64 | 2/6/2025 |
1.0.1-pre.15 | 69 | 2/5/2025 |
1.0.1-pre.14 | 65 | 2/5/2025 |
1.0.1-pre.13 | 60 | 2/3/2025 |
1.0.1-pre.12 | 66 | 2/3/2025 |
1.0.1-pre.11 | 69 | 2/1/2025 |
1.0.1-pre.10 | 64 | 1/30/2025 |
1.0.1-pre.9 | 70 | 1/28/2025 |
1.0.1-pre.8 | 63 | 1/26/2025 |
1.0.1-pre.7 | 59 | 1/24/2025 |
1.0.1-pre.6 | 64 | 1/22/2025 |
1.0.1-pre.5 | 55 | 1/20/2025 |
1.0.1-pre.4 | 55 | 1/18/2025 |
1.0.1-pre.3 | 55 | 1/16/2025 |
1.0.1-pre.2 | 48 | 1/14/2025 |
1.0.1-pre.1 | 58 | 1/13/2025 |
1.0.0 | 103 | 1/10/2025 |
0.0.1-pre.1 | 59 | 1/11/2025 |
## v1.1.3
Initial release or repository with no prior history.