Class Optional<T>
Represents an optional item of type
T
.
Inheritance
System.Object
Optional<T>
Inherited Members
System.Object.Equals(System.Object, System.Object)
System.Object.GetType()
System.Object.MemberwiseClone()
System.Object.ReferenceEquals(System.Object, System.Object)
System.Object.ToString()
Namespace: Maki
Assembly: Maki.dll
Syntax
public sealed class Optional<T>
Type Parameters
Name | Description |
---|---|
T | Contained item type. |
Examples
The following example shows how to use an Optional object to represent a value or absence of a value.using Maki;
using System;
namespace Samples
{
class Program
{
// Return a value only if a condition is met, otherwise Nothing
static Optional<int> TryGet()
{
var value = new Random().Next(100);
return value < 50 ? Optional.Make(value) : Optional.Nothing;
}
static void Main(string[] args)
{
var maybeInt = TryGet();
// HasValue checks for presence of the value
if (maybeInt.HasValue)
{
// Get() returns the value from the Optional
Console.WriteLine(maybeInt.Get());
}
else
{
Console.WriteLine("maybeInt is empty");
}
}
}
}
Constructors
| Improve this Doc View SourceOptional()
Creates an empty Optional.
Declaration
public Optional()
Optional(T)
Creates an Optional holding the given item.
Declaration
public Optional(T item)
Parameters
Type | Name | Description |
---|---|---|
T | item | Item to place into the Optional. |
Properties
| Improve this Doc View SourceHasValue
True if the optional holds an item.
Declaration
public bool HasValue { get; }
Property Value
Type | Description |
---|---|
System.Boolean |
Nothing
Represtens an empty Optional of the given type
T
.
Declaration
public static Optional<T> Nothing { get; }
Property Value
Type | Description |
---|---|
Optional<T> |
Methods
| Improve this Doc View SourceEquals(Object)
Determines whether the specified object is equal to the current object.
Declaration
public override bool Equals(object obj)
Parameters
Type | Name | Description |
---|---|---|
System.Object | obj | The object to compare with the current object. |
Returns
Type | Description |
---|---|
System.Boolean | True if the objects are equal. |
Overrides
System.Object.Equals(System.Object)
|
Improve this Doc
View Source
Get()
Gets the item held by this Optional.
Declaration
public T Get()
Returns
Type | Description |
---|---|
T | Item held by this Optional. |
Exceptions
Type | Condition |
---|---|
System.InvalidCastException | Thrown when the Optional does not hold an item. |
GetHashCode()
Returns the hash code for this instance.
Declaration
public override int GetHashCode()
Returns
Type | Description |
---|---|
System.Int32 | Hash code. |
Overrides
System.Object.GetHashCode()
Operators
| Improve this Doc View SourceExplicit(Optional<T> to T)
Explicitly casts from the given Optional to the held item type.
Declaration
public static explicit operator T(Optional<T> optional)
Parameters
Type | Name | Description |
---|---|---|
Optional<T> | optional | Optional to cast from. |
Returns
Type | Description |
---|---|
T |
Implicit(T to Optional<T>)
Implicitly casts from the given item to an Optional holding the item.
Declaration
public static implicit operator Optional<T>(T item)
Parameters
Type | Name | Description |
---|---|---|
T | item | Item to cast from. |
Returns
Type | Description |
---|---|
Optional<T> |
Implicit(Unit to Optional<T>)
Creates a new empty Optional.
Declaration
public static implicit operator Optional<T>(Unit unit)
Parameters
Type | Name | Description |
---|---|---|
Unit | unit | Unit type, can be Optional.Nothing. |
Returns
Type | Description |
---|---|
Optional<T> |