ktsu.DelegateTransform
2.0.2
Prefix Reserved
dotnet add package ktsu.DelegateTransform --version 2.0.2
NuGet\Install-Package ktsu.DelegateTransform -Version 2.0.2
<PackageReference Include="ktsu.DelegateTransform" Version="2.0.2" />
<PackageVersion Include="ktsu.DelegateTransform" Version="2.0.2" />
<PackageReference Include="ktsu.DelegateTransform" />
paket add ktsu.DelegateTransform --version 2.0.2
#r "nuget: ktsu.DelegateTransform, 2.0.2"
#:package ktsu.DelegateTransform@2.0.2
#addin nuget:?package=ktsu.DelegateTransform&version=2.0.2
#tool nuget:?package=ktsu.DelegateTransform&version=2.0.2
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.
Important: All With methods return a new transformed value - they never mutate the original input. This provides functional programming semantics even when using reference-based delegates.
Features
- ActionRef Transformations: Modify copies of values by reference using action delegates
- Func Transformations: Transform values using function delegates
- FuncRef Transformations: Transform values by reference using function delegates that return values
- Copy-on-Transform: Original values are never mutated
- Generic Implementation: Works with any value or reference type
Supported Frameworks
- .NET 10.0, 9.0, 8.0, 7.0, 6.0
- .NET Standard 2.1
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<T> delegate to apply a transformation by reference and return the result:
using ktsu.DelegateTransform;
int input = 5;
int result = input.With((ref int x) => x *= 2);
// result is 10, input is still 5 (original unchanged)
With Func
The With method can be used with a Func<T, TResult> delegate to transform the input value. The result can be the same type as the input or a different one:
using ktsu.DelegateTransform;
int input = 5;
int result = input.With(x => x * 2);
// result is 10
With FuncRef
The With method can be used with a FuncRef<T> delegate to transform the input value by reference:
using ktsu.DelegateTransform;
int input = 5;
int result = input.With((ref int x) => x * x);
// result is 25, input is still 5 (original unchanged)
Chaining Transformations
Every With method is an extension method that returns the transformed value, so calls chain fluently:
using ktsu.DelegateTransform;
string result = "example"
.With(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 (for reference types, the object is modified)
Person updatedPerson = person.With((ref Person p) => {
p.Name = p.Name.ToUpper();
p.Age += 1;
});
Console.WriteLine($"{updatedPerson.Name}, {updatedPerson.Age}"); // Outputs: JOHN, 31
// Transform using Func to create a string description
string description = person.With(p => $"{p.Name} is {p.Age} years old");
Console.WriteLine(description); // Outputs: JOHN is 31 years old
API Reference
Delegate Types
// Custom delegate for actions on references
public delegate void ActionRef<T>(ref T item);
// Custom delegate for functions on references
public delegate T FuncRef<T>(ref T item);
DelegateTransform Static Class
The static class holding the With extension methods. Call them on the value, as in input.With(...).
Naming the class, as in DelegateTransform.With(input, ...), does not compile from other ktsu.*
namespaces, where DelegateTransform resolves to the namespace of the same name.
Methods
| Method | Parameters | Return Type | Description |
|---|---|---|---|
With<T> |
this T input, ActionRef<T> delegate |
T |
Creates a copy, applies the action by reference, returns the modified copy |
With<T, TResult> |
this T input, Func<T, TResult> delegate |
TResult |
Applies a function to the input and returns the result, which may be a different type |
With<T> |
this T input, FuncRef<T> delegate |
T |
Passes input by reference to the function and returns the result |
All methods throw ArgumentNullException if the delegate is null.
Design Principles
Copy-on-Transform
The library follows a functional programming pattern where the original input is never mutated:
public static T With<T>(this T input, ActionRef<T> @delegate)
{
Ensure.NotNull(@delegate);
T output = input; // Creates a copy
@delegate(ref output);
return output;
}
This ensures predictable behavior and prevents side effects on the original values.
Performance Considerations
For value types (structs), a copy is made before transformation. For large structs where you want to avoid copying, consider using the FuncRef<T> overload which passes by reference and returns the modified value.
For reference types (classes), the "copy" is actually a copy of the reference, so the underlying object can be modified. If you need true immutability for reference types, create a new instance within your delegate.
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 | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. 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 is compatible. 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. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- System.Memory (>= 4.6.3)
- System.Threading.Tasks.Extensions (>= 4.6.3)
-
.NETStandard 2.1
- No dependencies.
-
net10.0
- No dependencies.
-
net8.0
- No dependencies.
-
net9.0
- 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 |
|---|---|---|
| 2.0.2 | 101 | 10/12/2026 |
| 2.0.1 | 102 | 10/9/2026 |
| 2.0.0 | 95 | 10/9/2026 |
| 1.1.42-pre.3 | 64 | 10/8/2026 |
| 1.1.42-pre.2 | 70 | 10/5/2026 |
| 1.1.42-pre.1 | 75 | 9/30/2026 |
| 1.1.41 | 137 | 9/29/2026 |
| 1.1.41-pre.1 | 62 | 9/28/2026 |
| 1.1.40 | 117 | 9/25/2026 |
| 1.1.39 | 107 | 9/24/2026 |
| 1.1.38 | 111 | 9/23/2026 |
| 1.1.37 | 112 | 9/22/2026 |
| 1.1.36 | 110 | 9/21/2026 |
| 1.1.35 | 123 | 9/18/2026 |
| 1.1.34 | 110 | 9/17/2026 |
| 1.1.33 | 114 | 9/16/2026 |
| 1.1.32 | 120 | 9/14/2026 |
| 1.1.31 | 116 | 9/14/2026 |
| 1.1.30 | 108 | 9/11/2026 |
| 1.1.29 | 134 | 9/4/2026 |
## v2.0.2 (patch)
Changes since v2.0.1:
- Suppress S3236 for the explicit nameof(@delegate) argument ([@Claude](https://github.com/Claude))
- Name the delegate parameter without its verbatim @ in ArgumentNullException [patch] ([@Claude](https://github.com/Claude))