Created
October 12, 2016 05:37
-
-
Save iwashihead/fe024bb199f678c5ee9408b5bea40db3 to your computer and use it in GitHub Desktop.
【C#】nullチェックを楽にする演算子のオーバーロード ref: http://qiita.com/iwashihead/items/7757003712b64a1f6fa8
This file contains 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
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; | |
} | |
} |
This file contains 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
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