Repository navigation
OnixLabs.Core.Success T
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>
|
public sealed class Success<T> : Result<T>| 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>.
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 byResult<T>.Success(T value)or the implicit conversionT → Result<T>. -
Value access. The
Valueproperty exposes the carried value directly. For safe, fluent access use the base-class accessors such asGetValueOrDefault()andGetValueOrThrow(). -
Disposal. When
Result<T>.Dispose()orResult<T>.DisposeAsync()is called on aSuccess<T>, disposal is forwarded toValueif it implementsIDisposableorIAsyncDisposablerespectively. -
Equality. Two
Success<T>instances are equal when theirValues are equal underResultEqualityComparer<T>. ASuccess<T>is never equal to aFailure<T>. -
ToString. Returns
Value.ToString(), orstring.EmptywhenValueisnull. -
Pattern matching. Use
if (result is Success<T> s)orresult.Match(...)to branch on success.
| Property | Type | Description |
|---|---|---|
Value |
T |
Gets the underlying value representing the successful result. |
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.
Success<T> inherits all public members of Result<T>, including:
- Static factories:
Of,OfAsync,Success,Failure - Properties:
IsSuccess(alwaystrue),IsFailure(alwaysfalse) - Accessors:
GetValueOrDefault(),GetValueOrThrow(),GetValue(out T) - Exception accessors:
GetExceptionOrDefault()(returnsnull),GetExceptionOrThrow()(throws) - Combinators:
Match,MatchAsync,Select,SelectAsync,SelectMany,SelectManyAsync - Operators:
==,!=,implicit T → Result<T>,implicit Exception → Result<T>,explicit Result<T> → T -
Dispose(),DisposeAsync(),Throw(),Equals(),GetHashCode(),ToString()
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"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>ONIXLabs .NET Library · Repository · NuGet · License (Apache-2.0)
Copyright © ONIXLabs 2020. Licensed under the Apache License, Version 2.0.
- Enumeration<T>
- Optional<T>
- OptionalEqualityComparer<T>
- OptionalExtensions
- Result
- Result<T>
- ResultEqualityComparer<T>
- ResultExtensions
- Specification<T>
- Preconditions
- IValueEquatable<T>
- IValueComparable<T>
- IBinaryConvertible
- ISpanBinaryConvertible
- IMemoryBinaryConvertible
- ArrayExtensions
- ComparableExtensions
- DateTimeExtensions
- ISpanParsableExtensions
- ObjectExtensions
- RandomExtensions
- ReadOnlySequenceExtensions
- ReadOnlySpanExtensions
- StringExtensions
- Base16
- Base32
- Base58
- Base64
- IBaseCodec
- IBaseValue
- IBaseValue<T>
- Base16Codec
- Base32Codec
- Base58Codec
- Base64Codec
- Base16FormatProvider
- Base32FormatProvider
- Base58FormatProvider
- Base64FormatProvider
- EncodingExtensions
- ReadOnlySpanExtensions
- StringBuilderExtensions
- BigDecimal
- BigDecimalEqualityComparer
- BigDecimalOrdinalityComparer
- NumberInfo
- NumberInfoEqualityComparer
- NumberInfoOrdinalityComparer
- Int256
- Int512
- UInt256
- UInt512
- Float128
- Float256
- GenericMath
- NumericsExtensions
- ConversionMode
- ScaleMode
- Hash
- NamedHash
- Sha3
- Sha3Hash224
- Sha3Hash256
- Sha3Hash384
- Sha3Hash512
- Sha3Shake128
- Sha3Shake256
- MerkleTree
- MerkleTree<T>
- PrivateKey
- PublicKey
- NamedPrivateKey
- NamedPublicKey
- EcdhPrivateKey
- EcdhPublicKey
- EcdsaPrivateKey
- EcdsaPublicKey
- EddsaPrivateKey
- EddsaPublicKey
- RsaPrivateKey
- RsaPublicKey
- DigitalSignature
- DigitalSignatureAndPublicKey
- Salt
- Secret
- ICryptoPrimitive<T>
- IHashable
- IEcdhPrivateKey
- IEcdsaPrivateKey
- IEddsaPrivateKey
- IRsaPrivateKey
- IPrivateKeyDerivable<T>
- IPrivateKeyExportable
- IPrivateKeyImportable<T>
- IPrivateKeyPemImportable<T>
- IPrivateKeyPkcs8Exportable
- IPrivateKeyPkcs8Importable<T>
- IPrivateKeyPkcs8PemExportable
- IPrivateKeyRawExportable
- IPrivateKeyRawImportable<T>
- IPrivateKeyRawPemExportable
- IEcdhPublicKey
- IEcdsaPublicKey
- IEddsaPublicKey
- IRsaPublicKey
- IPublicKeyExportable
- IPublicKeyImportable<T>
- IPublicKeyPemExportable
- IPublicKeyPemImportable<T>
- IPublicKeyRawExportable
- IPublicKeyRawImportable<T>
- HashAlgorithmExtensions
- ReadOnlySpanExtensions