Skip to content
This repository was archived by the owner on Sep 29, 2024. It is now read-only.
Sascha Manns edited this page Sep 6, 2024 · 1 revision

Result

This class can be used for using the result pattern.

Usage

public Result<string> GetUserNameById(int userId)
{
    if (userId <= 0)
    {
        return Result<string>.Failure("Invalid user ID");
    }

    // We simulate obtaining a username
    string userName = "John Doe"; // This would be the actual logic to get the name

    return Result<string>.Success(userName);
}

public void ProcessUserName()
{
    var result = GetUserNameById(1);

    if (result.IsSuccess)
    {
        Console.WriteLine($"User name: {result.Value}");
    }
    else
    {
        Console.WriteLine($"Error: {result.ErrorMessage}");
    }
}

More

More about that on: (https://medium.com/@davisaac8/an-alternative-to-try-catch-in-c-b0e5dfafa910)

Clone this wiki locally