Struct NotNull<T>
Represents an object that cannot be null.
Inherited Members
System.ValueType.ToString()
System.Object.Equals(System.Object, System.Object)
System.Object.GetType()
System.Object.ReferenceEquals(System.Object, System.Object)
Namespace: Maki
Assembly: Maki.dll
Syntax
public struct NotNull<T>
Type Parameters
| Name | Description |
|---|---|
| T | Type of object. |
Examples
The following example shows how to unpack a potentially null value into a NotNull. Once wrapped in a NotNull, code does not need to perform any additional null checks.using Maki;
using System;
namespace Samples
{
class Program
{
// No need to check argument before using it, it can never be null
static void UseValue(NotNull<string> value)
{
Console.WriteLine(value.Item);
}
static void Main(string[] args)
{
// An instance that could be null
string valueOrNull = null;
if (DateTime.Today.DayOfWeek == DayOfWeek.Friday)
valueOrNull = "It's Friday!";
// Perform a null check before assigning to NotNull<>
if (valueOrNull == null)
return;
// value is guaranteed to be non-null
NotNull<string> value = valueOrNull;
// No need to null check from here on
UseValue(value);
}
}
}
Constructors
| Improve this Doc View SourceNotNull(T)
Creates a new NotNull from the given object.
Declaration
public NotNull(T item)
Parameters
| Type | Name | Description |
|---|---|---|
| T | item | Object to store. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException | Thrown if the argument is null. |
Properties
| Improve this Doc View SourceItem
Gets the object.
Declaration
public T Item { get; }
Property Value
| Type | Description |
|---|---|
| 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.ValueType.Equals(System.Object)
|
Improve this Doc
View Source
GetHashCode()
Returns the hash code for this instance.
Declaration
public override int GetHashCode()
Returns
| Type | Description |
|---|---|
| System.Int32 | Hash code. |
Overrides
System.ValueType.GetHashCode()
|
Improve this Doc
View Source
MakeOptional(T)
Creates an Optional from the given object which is either Nothing if the
object is null or NotNull if the object is not null.
Declaration
public static Optional<NotNull<T>> MakeOptional(T value)
Parameters
| Type | Name | Description |
|---|---|---|
| T | value | Object to store. |
Returns
| Type | Description |
|---|---|
| Optional<NotNull<T>> | NotNull if object is not null, Nothing otherwise. |
Operators
| Improve this Doc View SourceImplicit(T to NotNull<T>)
Creates a new NotNull from the given object.
Declaration
public static implicit operator NotNull<T>(T item)
Parameters
| Type | Name | Description |
|---|---|---|
| T | item | Object to store. |
Returns
| Type | Description |
|---|---|
| NotNull<T> |
Implicit(NotNull<T> to T)
Implicit conversion to contained type.
Declaration
public static implicit operator T(NotNull<T> notNull)
Parameters
| Type | Name | Description |
|---|---|---|
| NotNull<T> | notNull | NotNull instance. |
Returns
| Type | Description |
|---|---|
| T |