public class Result<T, E>
private readonly T value;
private readonly E error;
private readonly bool isSuccess;
public Result(T value, E error, bool isSuccess)
this.isSuccess = isSuccess;
public static Result<T, E> AsOk(T value)
return new Result<T, E>(value, default, true);
public static Result<T, E> AsError(E error)
return new Result<T, E>(default, error, false);
public T Value => this.value;
public E Error => this.error;
public bool IsSuccess => this.isSuccess;
public bool IsFailure => !this.isSuccess;
public string Tag { get; }
public Bar(string tag, int id)
public static class Program
Foo foo = new Foo("tag1");
Bar bar = new Bar("tag2", 2);
Foo barAsFoo = new Bar("tag3", 3);
Result<Foo, int> triedFoo = Result<Foo, int>.AsOk(foo);
Result<Bar, int> triedBar = Result<Bar, int>.AsOk(bar);
Result<Foo, int> triedBarAsTriedFoo = triedBar;