Skip to content

Instantly share code, notes, and snippets.

@iwashihead
Created October 12, 2016 05:37
Show Gist options
  • Save iwashihead/fe024bb199f678c5ee9408b5bea40db3 to your computer and use it in GitHub Desktop.
Save iwashihead/fe024bb199f678c5ee9408b5bea40db3 to your computer and use it in GitHub Desktop.
【C#】nullチェックを楽にする演算子のオーバーロード ref: http://qiita.com/iwashihead/items/7757003712b64a1f6fa8
public class Hoge
{
public static implicit operator bool(Hoge self)
{
return self != null;
}
public static bool operator true(Hoge self)
{
return self != null;
}
public static bool operator false(Hoge self)
{
return self == null;
}
}
public class Main
{
private Hoge hoge;
public Main(object[] args)
{
if (hoge) { Console.WriteLine(hoge.ToString()); }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment