Created
October 16, 2013 21:14
-
-
Save jamesrcounts/7014994 to your computer and use it in GitHub Desktop.
Something guaranteed not to be null, opposite of Nullable<T>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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