Skip to content

OnixLabs.Core.Success

Matthew Layton edited this page Nov 15, 2026 · 2 revisions

Success Class

Represents a successful, non-generic result.

Namespace OnixLabs.Core
Assembly OnixLabs.Core.dll (NuGet)
Inheritance object → Result → Success
Implements IValueEquatable<Result>

Definition

public sealed class Success : Result

Success has no public constructor; obtain the shared instance via Result.Success(). All successful non-generic results share a single singleton instance — there is nothing to distinguish one success from another, so allocating multiple instances would be wasteful.

Success is one of exactly two sealed derivations of Result. The other is Failure. Because Result is a closed class, a C# 15 switch over Success and Failure is exhaustive without a discard arm.

Remarks

Success represents the outcome of an operation that completed without throwing an exception. Because it carries no payload, it is effectively a unit type: the only information it conveys is that the operation succeeded.

All Success instances compare equal to one another. Result.GetHashCode returns 0 for a Success, and Result.ToString returns string.Empty.

Success inherits the full combinator API — Select, SelectMany, Match, Throw, and their async variants — from Result. When the current instance is a Success, those combinators execute their delegate; when cast to Failure, they short-circuit. See the Result page for the complete inherited member list.

To promote an untyped Success to a typed Success<TResult>, use ToTypedResult<TResult>(TResult).

Examples

Obtaining a Success

using OnixLabs.Core;

// Via the factory method (returns the singleton).
Success success = Result.Success();

// Via Result.Of — returns Success when the action does not throw.
Result result = Result.Of(() => File.Delete("/tmp/scratch.txt"));
Console.WriteLine(result.IsSuccess); // True

Promoting to a typed Success

using OnixLabs.Core;

Success untyped = Result.Success();
Success<int> typed = untyped.ToTypedResult(42);
Console.WriteLine(typed.IsSuccess); // True

Pattern matching against Success

using OnixLabs.Core;

Result result = Result.Of(DoWork);

if (result is Success)
    Console.WriteLine("Work completed successfully.");

Methods

Method Returns Description
ToTypedResult<TResult>(TResult) Success<TResult> Promotes this success to a typed success carrying the specified value.

ToTypedResult<TResult>(TResult)

public Success<TResult> ToTypedResult<TResult>(TResult value)

Obtains a new Success<TResult> containing the specified value, promoting an untyped success to a typed one. This is the bridge from the non-generic result model to the generic Result<T> model.

Parameter Type Description
value TResult The underlying successful result value.

Returns — A new Success<TResult> containing value.

See Also

ONIXLabs .NET Library


Clone this wiki locally