Skip to content

πŸš€ Mind-bending custom operators for C# 14 that will revolutionize how you write code! Pipe, compose, and navigate safely with operators that make C# as expressive as functional languages. Say goodbye to boilerplate and hello to elegant, powerful syntax!

Notifications You must be signed in to change notification settings

MrEshboboyev/custom-operators-csharp-language

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Custom Operators in C# 14

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.

πŸš€ Features

1. Pipe Operator (|)

Chain operations seamlessly, transforming data through a series of functions:

var result = "18" | int.Parse | (x => x * 2); // Returns 36

2. Composition Operators (>> and <<)

Create 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"

3. Safe Navigation Operator (%)

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 null

4. Maybe Operator (&)

Handle 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"

5. Apply Operator (^)

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 11

πŸ§ͺ Real-World Examples

The 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

πŸ› οΈ Implementation Details

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.

🎯 Benefits

  • 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

πŸ“¦ Getting Started

  1. Clone the repository
  2. Open in Visual Studio or VS Code
  3. Run with dotnet run
  4. Explore the examples in Program.cs

πŸ“š Learn More

Check out Program.cs for comprehensive examples of each operator in action, demonstrating real-world scenarios and best practices.

🀝 Contributing

Feel free to contribute new operators, improve existing ones, or suggest enhancements!

πŸ“„ License

MIT License - See LICENSE file for details.

About

πŸš€ Mind-bending custom operators for C# 14 that will revolutionize how you write code! Pipe, compose, and navigate safely with operators that make C# as expressive as functional languages. Say goodbye to boilerplate and hello to elegant, powerful syntax!

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages