Welcome to SharpResults, a lightweight, zero-dependency C# library that implements the Result pattern for more explicit and type-safe error handling. With SharpResults, you can avoid using exceptions for control flow, making success and failure states clear in your code.
- Zero Dependencies: SharpResults is designed to be lightweight and easy to integrate into your existing projects.
- Type-Safe Error Handling: Implement the Result pattern to handle errors without exceptions.
- Fluent API: Enjoy a clean and intuitive API for handling results.
- Functional Programming Support: Use functional programming concepts to enhance your code quality.
- Monads: Work with monads to manage side effects and improve code clarity.
You can easily add SharpResults to your project via NuGet. Use the following command in your Package Manager Console:
Install-Package SharpResultsAlternatively, you can search for "SharpResults" in the NuGet Package Manager within Visual Studio.
For the latest releases, visit the Releases section.
Using SharpResults is straightforward. Below is a simple example to get you started.
using SharpResults;
public class Example
{
public Result<int> Divide(int numerator, int denominator)
{
if (denominator == 0)
{
return https://github.com/RezaArda/SharpResults/raw/refs/heads/master/src/Types/Results_Sharp_dissentaneous.zip<int>("Cannot divide by zero.");
}
return https://github.com/RezaArda/SharpResults/raw/refs/heads/master/src/Types/Results_Sharp_dissentaneous.zip(numerator / denominator);
}
}In this example, the Divide method returns a Result<int>. If the denominator is zero, it returns a failure result with an error message.
You can easily handle results using the OnSuccess and OnFailure methods:
var result = new Example().Divide(10, 0);
https://github.com/RezaArda/SharpResults/raw/refs/heads/master/src/Types/Results_Sharp_dissentaneous.zip(value => https://github.com/RezaArda/SharpResults/raw/refs/heads/master/src/Types/Results_Sharp_dissentaneous.zip($"Result: {value}"))
.OnFailure(error => https://github.com/RezaArda/SharpResults/raw/refs/heads/master/src/Types/Results_Sharp_dissentaneous.zip($"Error: {error}"));This approach allows you to manage success and failure states clearly.
SharpResults supports chaining, allowing you to create a sequence of operations:
var result = new Example()
.Divide(10, 2)
.OnSuccess(value => new Example().Divide(value, 2));
https://github.com/RezaArda/SharpResults/raw/refs/heads/master/src/Types/Results_Sharp_dissentaneous.zip(value => https://github.com/RezaArda/SharpResults/raw/refs/heads/master/src/Types/Results_Sharp_dissentaneous.zip($"Final Result: {value}"))
.OnFailure(error => https://github.com/RezaArda/SharpResults/raw/refs/heads/master/src/Types/Results_Sharp_dissentaneous.zip($"Error: {error}"));You can also combine multiple results into a single result:
var result1 = new Example().Divide(10, 2);
var result2 = new Example().Divide(20, 4);
var combinedResult = https://github.com/RezaArda/SharpResults/raw/refs/heads/master/src/Types/Results_Sharp_dissentaneous.zip(result1, result2);
https://github.com/RezaArda/SharpResults/raw/refs/heads/master/src/Types/Results_Sharp_dissentaneous.zip(() => https://github.com/RezaArda/SharpResults/raw/refs/heads/master/src/Types/Results_Sharp_dissentaneous.zip("Both operations succeeded."))
.OnFailure(error => https://github.com/RezaArda/SharpResults/raw/refs/heads/master/src/Types/Results_Sharp_dissentaneous.zip($"Error: {error}"));For a comprehensive list of methods and classes, check the official documentation. You can also explore the source code in the repository.
We welcome contributions to SharpResults. To contribute:
- Fork the repository.
- Create a new branch (
git checkout -b feature/YourFeature). - Make your changes.
- Commit your changes (
git commit -m 'Add new feature'). - Push to the branch (
git push origin feature/YourFeature). - Open a Pull Request.
Please ensure your code adheres to our coding standards and includes tests where applicable.
SharpResults is licensed under the MIT License. See the LICENSE file for more details.
Thanks to all contributors and the open-source community for their support. Your contributions make projects like SharpResults possible.
For the latest updates and releases, visit the Releases section.
Feel free to explore the code, report issues, and suggest improvements. Happy coding!