Skip to content

Instantly share code, notes, and snippets.

@jamesrcounts
Created October 16, 2013 21:14
Show Gist options
  • Select an option

  • Save jamesrcounts/7014994 to your computer and use it in GitHub Desktop.

Select an option

Save jamesrcounts/7014994 to your computer and use it in GitHub Desktop.
Something guaranteed not to be null, opposite of Nullable<T>
using System;
using System.Diagnostics.Contracts;
public struct NonNullable<T> where T : class
{
private readonly T data;
public NonNullable(T data)
{
if (data == null)
{
throw new ArgumentNullException("data");
}
this.data = data;
}
public T Data
{
get
{
Contract.Ensures(Contract.Result<T>() != null);
return this.data;
}
}
[ContractInvariantMethod]
private void ObjectInvariant()
{
Contract.Invariant(this.data != null);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment