Class ParseResult<T>
The result of a parse operation.
Since 1.0.x
Availability net45, netstandard1.3
Inheritance
Object
ParseResult<T>
Inherited Members
Object.ToString()
Object.Equals(Object)
Object.Equals(Object, Object)
Object.ReferenceEquals(Object, Object)
Object.GetHashCode()
Object.GetType()
Object.MemberwiseClone()
Namespace: NodaTime.Text
Assembly: NodaTime.dll
Syntax
public sealed class ParseResult<T>
Type Parameters
Name | Description |
---|---|
T | The type which was parsed, such as a LocalDateTime. |
Properties
Exception
Gets an exception indicating the cause of the parse failure.
Since 1.2.x
Availability net45, netstandard1.3
Declaration
public Exception Exception { get; }
Property Value
Type | Description |
---|---|
Exception | The exception indicating the cause of the parse failure. (The value returned is never null.) |
Remarks
This property is typically used to wrap parse failures in higher level exceptions.
Exceptions
Type | Condition |
---|---|
System.InvalidOperationException | The parse operation succeeded. |
Success
Indicates whether the parse operation was successful.
Since 1.0.x
Availability net45, netstandard1.3
Declaration
public bool Success { get; }
Property Value
Type | Description |
---|---|
Boolean | true if the parse operation was successful; otherwise false. |
Remarks
This returns True if and only if fetching the value with the Value property will return with no exception.
Value
Gets the value from the parse operation if it was successful, or throws an exception indicating the parse failure
otherwise.
Since 1.0.x
Availability net45, netstandard1.3
Declaration
public T Value { get; }
Property Value
Type | Description |
---|---|
T | The result of the parsing operation if it was successful. |
Remarks
This method is exactly equivalent to calling the GetValueOrThrow() method, but is terser if the code is
already clear that it will throw if the parse failed.
Methods
Convert<TTarget>(Func<T, TTarget>)
Converts this result to a new target type, either by executing the given projection
for a success result, or propagating the exception provider for failure.
Since 2.1.x
Availability net45, netstandard1.3
Declaration
public ParseResult<TTarget> Convert<TTarget>(Func<T, TTarget> projection)
Parameters
Type | Name | Description |
---|---|---|
Func<T, TTarget> | projection | The projection to apply for the value of this result, if it's a success result. |
Returns
Type | Description |
---|---|
ParseResult<TTarget> | A ParseResult for the target type, either with a value obtained by applying the specified projection to the value in this result, or with the same error as this result. (The value returned is never null.) |
Type Parameters
Name | Description |
---|---|
TTarget |
Exceptions
Type | Condition |
---|---|
ArgumentNullException | projection is null. |
ConvertError<TTarget>()
Converts this result to a new target type by propagating the exception provider.
This parse result must already be an error result.
Since 2.1.x
Availability net45, netstandard1.3
Declaration
public ParseResult<TTarget> ConvertError<TTarget>()
Returns
Type | Description |
---|---|
ParseResult<TTarget> | A ParseResult for the target type, with the same error as this result. (The value returned is never null.) |
Type Parameters
Name | Description |
---|---|
TTarget |
ForException(Func<Exception>)
Produces a ParseResult which represents a failed parsing operation.
Since 2.1.x
Availability net45, netstandard1.3
Declaration
public static ParseResult<T> ForException(Func<Exception> exceptionProvider)
Parameters
Type | Name | Description |
---|---|---|
Func<Exception> | exceptionProvider | A delegate that produces the exception representing the error that caused the parse to fail. |
Returns
Type | Description |
---|---|
ParseResult<T> | A ParseResult representing a failed parsing operation. (The value returned is never null.) |
Remarks
This method accepts a delegate rather than the exception itself, as creating an
exception can be relatively slow: if the client doesn't need the actual exception, just the information
that the parse failed, there's no point in creating the exception.
Exceptions
Type | Condition |
---|---|
ArgumentNullException | exceptionProvider is null. |
ForValue(T)
Produces a ParseResult which represents a successful parse operation.
Since 2.1.x
Availability net45, netstandard1.3
Declaration
public static ParseResult<T> ForValue(T value)
Parameters
Type | Name | Description |
---|---|---|
T | value | The successfully parsed value. |
Returns
Type | Description |
---|---|
ParseResult<T> | A ParseResult representing a successful parsing operation. (The value returned is never null.) |
Remarks
When T is a reference type,
value
should not be null,
but this isn't currently checked.GetValueOrThrow()
Gets the value from the parse operation if it was successful, or throws an exception indicating the parse failure
otherwise.
Since 1.0.x
Availability net45, netstandard1.3
Declaration
public T GetValueOrThrow()
Returns
Type | Description |
---|---|
T | The result of the parsing operation if it was successful. |
Remarks
This method is exactly equivalent to fetching the Value property, but more explicit in terms of throwing
an exception on failure.
TryGetValue(T, out T)
Returns the success value, and sets the out parameter to either
the specified failure value of T or the successful parse result value.
Since 1.0.x
Availability net45, netstandard1.3
Declaration
public bool TryGetValue(T failureValue, out T result)
Parameters
Type | Name | Description |
---|---|---|
T | failureValue | The "default" value to set in result if parsing failed. |
T | result | The parameter to store the parsed value in on success. |
Returns
Type | Description |
---|---|
Boolean | True if this parse result was successful, or false otherwise. |