Skip to content

Instantly share code, notes, and snippets.

@riyadparvez
Created January 6, 2013 09:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save riyadparvez/4466210 to your computer and use it in GitHub Desktop.
Save riyadparvez/4466210 to your computer and use it in GitHub Desktop.
Non-nullable reference types. It's from Jon Skeet
public struct NonNullable<T> where T : class
{
private readonly T value;
public NonNullable(T value)
{
if (value == null)
{
throw new ArgumentNullException("value");
}
this.value = value;
}
public T Value
{
get
{
if (value == null)
{
throw new NullReferenceException();
}
return value;
}
}
public static implicit operator NonNullable<T>(T value)
{
return new NonNullable<T>(value);
}
public static implicit operator T(NonNullable<T> wrapper)
{
return wrapper.Value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment