Skip to content

OnixLabs.Core.Success T

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

Success<T> Class

Represents a successful generic result that carries the underlying value of type T.

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

Definition

public sealed class Success<T> : Result<T>

Type Parameters

Parameter Description
T The type of the underlying value carried by this successful result.

Success<T> has no public constructor. Obtain an instance via Result<T>.Success(T) or the implicit conversion from T to Result<T>.

Remarks

Success<T> is the success rail of the railway-oriented Result<T> pattern. It is one of exactly two sealed derivations of the closed base class Result<T> (the other being Failure<T>), so a C# 15 switch over Success<T> and Failure<T> is exhaustive without a discard arm.

Key behaviours:

  • Construction. The constructor is internal; instances can only be created by Result<T>.Success(T value) or the implicit conversion T → Result<T>.
  • Value access. The Value property exposes the carried value directly. For safe, fluent access use the base-class accessors such as GetValueOrDefault() and GetValueOrThrow().
  • Disposal. When Result<T>.Dispose() or Result<T>.DisposeAsync() is called on a Success<T>, disposal is forwarded to Value if it implements IDisposable or IAsyncDisposable respectively.
  • Equality. Two Success<T> instances are equal when their Values are equal under ResultEqualityComparer<T>. A Success<T> is never equal to a Failure<T>.
  • ToString. Returns Value.ToString(), or string.Empty when Value is null.
  • Pattern matching. Use if (result is Success<T> s) or result.Match(...) to branch on success.

Properties

Property Type Description
Value T Gets the underlying value representing the successful result.

Value

public T Value { get; }

Gets the underlying value representing the successful result. Set once at construction; always non-null when T is a non-nullable type.

Inherited Members

Success<T> inherits all public members of Result<T>, including:

Examples

Creating and using a successful result

using OnixLabs.Core;

// Via static factory
Result<int> a = Result<int>.Success(42);

// Via implicit conversion from T
Result<int> b = 42;

// Inspect the value
if (b is Success<int> s)
    Console.WriteLine(s.Value); // 42

// Use the combinator API
string text = b.Match(
    success: v => $"Got {v}",
    failure: _ => "failed");
// text = "Got 42"

Disposal of a disposable value

using (Result<FileStream> result = Result<FileStream>.Of(() => File.OpenRead(path)))
{
    // result is Success<FileStream> if the file opened successfully
    result.Match(
        success: stream => Process(stream),
        failure: ex => Log(ex));
} // FileStream is disposed here if result is Success<FileStream>

See Also

ONIXLabs .NET Library


Clone this wiki locally