Show / Hide Table of Contents

Class Result

This class contains static methods for creating a Result<T, TErr>.

Inheritance
object
Result
Inherited Members
object.Equals(object)
object.Equals(object, object)
object.GetHashCode()
object.GetType()
object.MemberwiseClone()
object.ReferenceEquals(object, object)
object.ToString()
Namespace: RustyOptions
Assembly: RustyOptions.dll
Syntax
public static class Result

Methods

| Edit this page View Source

Err<T>(Exception)

Creates a Result<T, TErr> in the Err state, containing the given exception.

Declaration
public static Result<T, Exception> Err<T>(Exception ex) where T : notnull
Parameters
Type Name Description
Exception ex

The exception to store in the result.

Returns
Type Description
Result<T, Exception>

A result object containing the given exception.

Type Parameters
Name Description
T

The type of the value the result would contain if it were not in the Err state.

Exceptions
Type Condition
ArgumentNullException

Thrown if the exception is null.

| Edit this page View Source

Err<T>(string)

Creates a Result<T, TErr> in the Err state, containing the given error message.

Declaration
public static Result<T, string> Err<T>(string errMsg) where T : notnull
Parameters
Type Name Description
string errMsg

The error message to store in the result.

Returns
Type Description
Result<T, string>

A result object containing the given error message.

Type Parameters
Name Description
T

The type of the value the result would contain if it were not in the Err state.

Exceptions
Type Condition
ArgumentNullException

Thrown if the error message is null.

| Edit this page View Source

Err<T, TErr>(TErr)

Creates a Result<T, TErr> in the Err state, containing the given error value.

Declaration
public static Result<T, TErr> Err<T, TErr>(TErr error) where T : notnull
Parameters
Type Name Description
TErr error

The error value to store in the result.

Returns
Type Description
Result<T, TErr>

A result object containing the given error value.

Type Parameters
Name Description
T

The type of the value the result would contain if it were not in the Err state.

TErr

The type of the error the result contains.

Exceptions
Type Condition
ArgumentNullException

Thrown if the error value is null.

| Edit this page View Source

OkExn<T>(T)

Creates a Result<T, TErr> in the Ok state, with Exception as the error type.

This overload avoids explicit generic annotations when converting try/catch code into a Result.

Declaration
public static Result<T, Exception> OkExn<T>(T value) where T : notnull
Parameters
Type Name Description
T value

The value to store in the result.

Returns
Type Description
Result<T, Exception>

A result object containing the given value.

Type Parameters
Name Description
T

The type of the value the result contains.

Exceptions
Type Condition
ArgumentNullException

Thrown if the value is null.

| Edit this page View Source

Ok<T>(T)

Creates a Result<T, TErr> in the Ok state, with string as the error type.

This overload avoids explicit generic annotations when you want the error to be a simple message.

Declaration
public static Result<T, string> Ok<T>(T value) where T : notnull
Parameters
Type Name Description
T value

The value to store in the result.

Returns
Type Description
Result<T, string>

A result object containing the given value.

Type Parameters
Name Description
T

The type of the value the result contains.

Exceptions
Type Condition
ArgumentNullException

Thrown if the value is null.

| Edit this page View Source

Ok<T, TErr>(T)

Creates a Result<T, TErr> in the Ok state, containing the given value.

Declaration
public static Result<T, TErr> Ok<T, TErr>(T value) where T : notnull
Parameters
Type Name Description
T value

The value to store in the result.

Returns
Type Description
Result<T, TErr>

A result object containing the given value.

Type Parameters
Name Description
T

The type of value the result contains.

TErr

The type of error the result may contain.

Exceptions
Type Condition
ArgumentNullException

Thrown if the value is null.

| Edit this page View Source

Try<T>(Func<T>)

Attempts to call func, wrapping the returned value in an Ok result. Any exceptions will be caught and returned in an Err result.

Declaration
public static Result<T, Exception> Try<T>(Func<T> func) where T : notnull
Parameters
Type Name Description
Func<T> func

The function to attempt calling.

Returns
Type Description
Result<T, Exception>

The return value of func wrapped in Ok, or Err containing any exception that was thrown.

Type Parameters
Name Description
T

The type of the value returned by the given function.

  • Edit this page
  • View Source
In this article
Back to top Generated by DocFX