Skip to content

Instantly share code, notes, and snippets.

@jcbozonier
Created October 19, 2009 22:54
Show Gist options
  • Select an option

  • Save jcbozonier/213795 to your computer and use it in GitHub Desktop.

Select an option

Save jcbozonier/213795 to your computer and use it in GitHub Desktop.
public struct NotNull<T> where T : class
{
private readonly T _Value;
private NotNull(T value)
{
if (value == null)
throw new ArgumentNullException("value");
_Value = value;
}
public static explicit operator NotNull<T>(T value)
{
return new NotNull<T>(value);
}
public static explicit operator T(NotNull<T> value)
{
return value._Value;
}
public static bool operator ==(NotNull<T> obj1, NotNull<T> obj2)
{
return obj1.Equals(obj2);
}
public static bool operator !=(NotNull<T> obj1, NotNull<T> obj2)
{
return !(obj1 == obj2);
}
public override bool Equals(object obj)
{
if(obj is NotNull<T>)
return Equals((NotNull<T>)obj);
return _Value.Equals(obj);
}
public bool Equals(NotNull<T> other)
{
return Equals(other._Value, _Value);
}
public override int GetHashCode()
{
return _Value.GetHashCode();
}
public override string ToString()
{
return _Value.ToString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment