This repository showcases innovative custom operators in C# 14 that solve real-world programming challenges. Inspired by functional programming paradigms, these operators make C# code more expressive, concise, and powerful.
Chain operations seamlessly, transforming data through a series of functions:
var result = "18" | int.Parse | (x => x * 2); // Returns 36Create powerful function compositions for data transformation pipelines:
Func<string, int> parse = int.Parse;
Func<int, int> multiplyByTwo = x => x * 2;
Func<int, string> toString = x => $"Result: {x}";
// Forward composition
var process = parse >> multiplyByTwo >> toString;
Console.WriteLine(process("25")); // "Result: 50"
// Backward composition
var processBackward = toString << multiplyByTwo << parse;
Console.WriteLine(processBackward("30")); // "Result: 60"Safely navigate object properties and collections without null reference exceptions:
var person = new Person { Address = new Address { City = "New York" } };
var city = person % (p => p.Address?.City); // Returns "New York"
var hobbies = new List<string> { "Reading", "Swimming" };
var firstHobby = hobbies % 0; // Returns "Reading"
var missingHobby = hobbies % 10; // Returns nullHandle nullable values elegantly with fallback mechanisms:
string? nullableString = null;
var result = nullableString & "Default Value"; // Returns "Default Value"
string? nonNullString = "Hello";
var result2 = nonNullString & "Default Value"; // Returns "Hello"Apply functions to values with a clean, functional syntax:
Func<int, int> square = x => x * x;
var result = square ^ 5; // Returns 25
Func<string, int> length = s => s.Length;
var result2 = length ^ "Hello World"; // Returns 11The project includes comprehensive examples demonstrating how these operators solve common programming challenges:
- Data Transformation Pipelines: Chain operations for clean data processing
- Null-Safe Object Navigation: Eliminate null reference exceptions
- Functional Composition: Build complex operations from simple functions
- Error Handling: Gracefully handle missing or invalid data
All operators are implemented using C# 14's extension syntax, making them seamlessly available when the namespace is imported:
using CustomOperators; // That's it! All operators are now available.- Expressive Code: Write code that reads like natural language
- Reduced Boilerplate: Eliminate repetitive null checks and intermediate variables
- Functional Programming: Embrace functional paradigms in C#
- Type Safety: Maintain C#'s strong typing while extending functionality
- Performance: Zero overhead compared to traditional approaches
- Clone the repository
- Open in Visual Studio or VS Code
- Run with
dotnet run - Explore the examples in
Program.cs
Check out Program.cs for comprehensive examples of each operator in action, demonstrating real-world scenarios and best practices.
Feel free to contribute new operators, improve existing ones, or suggest enhancements!
MIT License - See LICENSE file for details.