public static void Main()
Console.WriteLine("Success?");
var result = ReturnSuccess();
Console.WriteLine("Success!");
Console.WriteLine("Successful result: {0}", result.Result);
Console.WriteLine("Failure!");
Console.WriteLine("My error: {0}: ", result.Error);
Console.WriteLine("My exception: {0}", result.Ex);
Console.WriteLine("\nError?");
if (result.Success){ Console.WriteLine("Success!"); }
Console.WriteLine("Error!");
Console.WriteLine("My error: {0}: ", result.Error);
Console.WriteLine("My exception: {0}", result.Ex);
Console.WriteLine("\nException?");
result = ReturnErrorAndException();
if (result.Success){ Console.WriteLine("Success!"); }
Console.WriteLine("Exception!");
Console.WriteLine("My error: {0}: ", result.Error);
Console.WriteLine("My exception: {0}", result.Ex);
public static ResultObject<string> ReturnError()
return new ResultObject<string>(Error: "hej");
public static ResultObject<string> ReturnSuccess()
return new ResultObject<string>(Result: "hej");
public static ResultObject<string> ReturnErrorAndException()
return new ResultObject<string>(Error: "hej", new ArgumentException());
public class ResultObject<T>
public ResultObject(T Result)
if (Result is null) { throw new ArgumentNullException(nameof(Result), $"{nameof(Result)} cannot be null"); }
public ResultObject(string Error)
if (string.IsNullOrWhiteSpace(Error)) { throw new ArgumentException("Error message can't be null, empty or only whitespace characters!"); }
public ResultObject(string Error, Exception Ex)
if (string.IsNullOrWhiteSpace(Error)) { throw new ArgumentException("Error message can't be null, empty or only whitespace characters!"); }
public bool Success { get => Result is not null; }
public T? Result { get; init; }
public string? Error { get; init; }
public Exception? Ex { get; init; }