{"id":3390,"date":"2012-05-07T12:00:36","date_gmt":"2012-05-06T18:00:36","guid":{"rendered":"http:\/\/www.moonmile.net\/blog\/archives\/3390"},"modified":"2012-05-08T02:04:15","modified_gmt":"2012-05-07T17:04:15","slug":"null%e3%83%9d%e3%82%a4%e3%83%b3%e3%82%bf%e3%83%bc%e3%81%8cok%e3%81%aa%e3%80%81%e3%82%aa%e3%83%96%e3%82%b8%e3%82%a7%e3%82%af%e3%83%88%e3%82%92c%e3%81%a7%e5%ae%9f%e8%a3%85%e3%81%99%e3%82%8b","status":"publish","type":"post","link":"https:\/\/www.moonmile.net\/blog\/archives\/3390","title":{"rendered":"null\u30dd\u30a4\u30f3\u30bf\u30fc\u304cok\u306a\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092C#\u3067\u5b9f\u88c5\u3059\u308b"},"content":{"rendered":"<p>WCF\u30b5\u30fc\u30d3\u30b9\u3092\u8abf\u3079\u3066\u3044\u308b\u3068\u304d\u306b\u898b\u3064\u3051\u305f\u306e\u3067\u3001\u3061\u3087\u3063\u3068\u30e1\u30e2\u7684\u306b\u3002\u4e0b\u8a18\u306e SafeInvoke \u30e1\u30bd\u30c3\u30c9\u306e\u3068\u3053\u308d\u3067\u3059\u3002<\/p>\n<p>Method call if not null in C# &#8211; Stack Overflow<br \/>\n<a href=\"http:\/\/stackoverflow.com\/questions\/872323\/method-call-if-not-null-in-c-sharp\">http:\/\/stackoverflow.com\/questions\/872323\/method-call-if-not-null-in-c-sharp<\/a><\/p>\n<p>objective-c \u306b\u306f\u4fbf\u5229\u306a\u6a5f\u80fd\u304c\u3042\u3063\u3066\u3001\u5909\u6570\u304c null \u306e\u5834\u5408\u306f\u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3073\u51fa\u3055\u306a\u3044\u306e\u3067\u3059\u3002\u3053\u306e\u305f\u3081\u306b null \u30c1\u30a7\u30c3\u30af\u304c\u3044\u308a\u307e\u305b\u3093\u3002\u5177\u4f53\u7684\u306b\u30b3\u30fc\u30c9\u3092\u793a\u3059\u3068\u3001<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\r\n NullObject *obj = &#x5B;NullObject new];\r\n &#x5B;obj callMethod];\r\n\r\n \/\/ null \u3092\u4ee3\u5165\r\n obj = NULL;\r\n \/\/ \u6b21\u306e\u95a2\u6570\u306f\u547c\u3073\u51fa\u3055\u308c\u306a\u3044\r\n &#x5B;obj CallMethod];\r\n<\/pre>\n<p>\u3063\u3066\u306a\u611f\u3058\u3067\u30012\u56de\u76ee\u306e CallMethod \u306f\u547c\u3073\u51fa\u3055\u308c\u307e\u305b\u3093\u3002 if ( obj != NULL ) \u3068\u3044\u3046\u30c1\u30a7\u30c3\u30af\u304c\u3044\u3089\u306a\u304f\u306a\u3063\u3066\u30b3\u30fc\u30c9\u304c\u30b7\u30f3\u30d7\u30eb\u306b\u306a\u308a\u307e\u3059\u3002\u307e\u3042\u3001\u53b3\u5bc6\u6027\u3092\u91cd\u3093\u3058\u308b\u306a\u3089\u3070 NULL \u30c1\u30a7\u30c3\u30af\u3092\u3059\u308b\u300c\u610f\u56f3\u300d\u306f\u6b8b\u3057\u3066\u304a\u3044\u305f\u307b\u3046\u304c\u3044\u3044\u306e\u3067\u3059\u304c\u3001\u30b3\u30fc\u30c9\u306e\u5b89\u5168\u6027\u3092\u8003\u3048\u308b\u3068\u3053\u308c\u3067 ok \u306a\u6c17\u304c\u3057\u307e\u3059\u3002<\/p>\n<p>\u25a0\u62e1\u5f35\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3046<\/p>\n<p>\u5b9f\u306f C# \u306e\u62e1\u5f35\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3046\u3068\u4f3c\u305f\u3088\u3046\u306a\u3053\u3068\u304c\u3067\u304d\u308b\u3001\u3068\u3044\u3046\u306e\u3092\u5148\u65e5\u77e5\u308a\u307e\u3057\u305f\u3002\u5143\u306e\u30af\u30e9\u30b9\u3092 NullObject \u306b\u3057\u3066\u3001\u62e1\u5f35\u30e1\u30bd\u30c3\u30c9\u3092\u542b\u3080\u30af\u30e9\u30b9\u3092 NullObjectExtention \u306b\u3057\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nnamespace SampleNullObjectExtention\r\n{\r\n    public partial class Form1 : Form\r\n    {\r\n        public Form1()\r\n        {\r\n            InitializeComponent();\r\n        }\r\n\r\n        private void button1_Click(object sender, EventArgs e)\r\n        {\r\n            var obj = new NullObject();\r\n            Debug.Print(&quot;goMessage pre&quot;);\r\n            obj.goMessage(&quot;masuda&quot;);\r\n            Debug.Print(&quot;goMessage after&quot;);\r\n\r\n            obj = null;\r\n\r\n            Debug.Print(&quot;goMessageEx pre&quot;);\r\n            obj.goMessageEx(&quot;tomoaki&quot;);\r\n            Debug.Print(&quot;goMessageEx pre&quot;);\r\n\r\n        }\r\n    }\r\n\r\n    public static class NullObjectExtention\r\n    {\r\n        public static void goMessageEx( this NullObject obj, string msg)\r\n        {\r\n            if (obj != null)\r\n            {\r\n                obj.goMessage(msg);\r\n            }\r\n        }\r\n    }\r\n    public class NullObject\r\n    {\r\n        public void goMessage(string msg)\r\n        {\r\n            Debug.Print(&quot;in goMessage {0}&quot;, msg);\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>\u3059\u308b\u3068\u3001button1 \u3092\u30af\u30ea\u30c3\u30af\u3057\u305f\u3068\u304d\u306b\u3001obj \u3092 null \u306b\u8a2d\u5b9a\u3057\u3066\u304a\u3044\u3066\u3082\u3001goMessageEx \u30e1\u30bd\u30c3\u30c9\u547c\u3073\u51fa\u3057\u306f\u5927\u4e08\u592b\u306a\u3093\u3067\u3059\u306d\u3002\u306a\u308b\u307b\u3069\u3002\u62e1\u5f35\u30e1\u30bd\u30c3\u30c9\u5074\u3067 this \u3067\u53c2\u7167\u3055\u305b\u3066 null \u30c1\u30a7\u30c3\u30af\u3092\u3059\u308b\u3068\u3044\u3046\u6280\u3067\u3059\u3002LINQ \u306e\u5185\u90e8\u3067\u3082\u4f7f\u3063\u3066\u3044\u308b\u306e\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002<\/p>\n<p>\u306a\u304b\u306a\u304b\u9762\u5012\u3067\u3059\u304c\u3001\u30e1\u30bd\u30c3\u30c9\u3092\u5168\u3066\u30e9\u30c3\u30d7\u3057\u3066\u3057\u307e\u3063\u3066\u3001\u30e9\u30c3\u30d7\u3057\u305f\u65b9\u306e\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3046\u3068\u3044\u3046\u30eb\u30fc\u30eb\u306b\u3059\u308c\u3070 ok \u306a\u3093\u3067\u3059\u304c\u3001\u3044\u3084\u3044\u3084\u3001\u305d\u3046\u306f\u3044\u304d\u307e\u305b\u3093\u3002\u30a4\u30f3\u30c6\u30ea\u30bb\u30f3\u30b9\u3067\u300cgoMessage\u300d\u304c\u51fa\u3066\u304f\u308b\u306a\u3089\u3070\u3001\u305d\u308c\u3092\u4f7f\u3063\u3066\u3057\u307e\u3046\u304b\u3082\u3057\u308c\u306a\u3044\u3002\u306a\u3089\u3070\u3001obj \u304c null \u306e\u5834\u5408\u306f\u901a\u5e38\u901a\u308a\u843d\u3061\u3066\u3057\u307e\u3046\u8a33\u3067\u3059\u3002\u4f7f\u3048\u306a\u3044\u6280\u3067\u3059\u306d\uff5e\u3068\u3044\u3046\u611f\u3058\u306b\u306a\u3063\u3066\u3057\u307e\u3044\u307e\u3059\u3002<\/p>\n<p>\u25a0\u5225\u306e\u30a2\u30bb\u30f3\u30d6\u30ea\u306b\u96a0\u3059\uff08internal protected\uff09<\/p>\n<p>C++\u306e\u3088\u3046\u306bfriend\u3067\u5236\u9650\u304c\u3067\u304d\u308c\u3070\u3088\u3044\u306e\u3067\u3059\u304c\u3001.NET\u306b\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u305d\u306e\u4ee3\u308a\u300cinternal protected\u300d\u3068\u3044\u3046\u3061\u3087\u3063\u3068\u4e2d\u9014\u534a\u7aef\u306a\uff08\u4fbf\u5229\u306a\uff09\u7bc4\u56f2\u8a2d\u5b9a\u304c\u3067\u304d\u307e\u3059\u3002<br \/>\n\u62e1\u5f35\u30e1\u30bd\u30c3\u30c9\u306f\u3001protected \u306a\u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3073\u51fa\u305b\u306a\u3044\u306e\u3067\u3001\u4ee3\u66ff\u6848\u3068\u3044\u3046\u8a33\u3067\u3059\u3002<\/p>\n<p>\u30c6\u30b9\u30c8\u7528\u306e\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3068\u306f\u5225\u306b\u3001\u30af\u30e9\u30b9\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nnamespace CheckNullObjectLib\r\n{\r\n    \/\/\/\r\n    \/\/\/ NullObject \u306e\u62e1\u5f35\u30af\u30e9\u30b9\r\n    \/\/\/\r\n    public static class NullObjectExtention\r\n    {\r\n        \/\/\/\r\n        \/\/\/ SetMessage \u306e Wrapper\r\n        \/\/\/\r\n        \/\/\/ &lt;param name=&amp;quot;me&amp;quot; \/&gt;\r\n        \/\/\/ &lt;param name=&amp;quot;msg&amp;quot; \/&gt;\r\n        public static void SetMessage(this NullObject me, string msg)\r\n        {\r\n            if (me != null)\r\n            {\r\n                me._setMessage(msg);\r\n            }\r\n        }\r\n        \/\/\/\r\n        \/\/\/ GetMessage \u306e Wrapper\r\n        \/\/\/\r\n        \/\/\/ &lt;param name=&amp;quot;me&amp;quot; \/&gt;\r\n        \/\/\/ &lt;param name=&amp;quot;msg&amp;quot; \/&gt;\r\n        public static string GetMessage(this NullObject me)\r\n        {\r\n            if (me != null)\r\n            {\r\n                return me._getMessage();\r\n            }\r\n            \/\/ \u3042\u3048\u3066\u7a7a\u6587\u5b57\u5217\u3092\u8fd4\u3059\r\n            return &amp;quot;&amp;quot;;\r\n        }\r\n    }\r\n    \/\/\/\r\n    \/\/\/ \u672c\u4f53\u306e NullObject \u30af\u30e9\u30b9\r\n    \/\/\/\r\n    public class NullObject\r\n    {\r\n        protected internal string _msg = &amp;quot;&amp;quot;;\r\n\r\n        \/\/\/\r\n        \/\/\/ \u30a2\u30af\u30bb\u30b9\u5236\u9650\u3092 protected internal\uff08\u540c\u3058\u30a2\u30bb\u30f3\u30d6\u30ea\u306e\u307f\uff09\u306b\u3057\u3066\u304a\u304f\r\n        \/\/\/\r\n        \/\/\/ &lt;param name=&amp;quot;msg&amp;quot; \/&gt;\r\n        protected internal void _setMessage(string msg)\r\n        {\r\n            _msg = msg;\r\n        }\r\n        protected internal string _getMessage()\r\n        {\r\n            return _msg;\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>\u672c\u4f53\u306e NullObject \u30af\u30e9\u30b9\u3067\u516c\u958b\u3059\u308b\u30e1\u30bd\u30c3\u30c9\/\u30d7\u30ed\u30d1\u30c6\u30a3\u306f\u3001\u5168\u3066 protected internal \u306b\u3057\u3066\u304a\u304d\u307e\u3059\u3002\u3053\u3046\u3059\u308b\u3068\u3001\u540c\u3058\u30a2\u30bb\u30f3\u30d6\u30ea\u5185\u306e\u30af\u30e9\u30b9\u304b\u3089\u306e\u307f\u30a2\u30af\u30bb\u30b9\u304c\u53ef\u80fd\u306b\u306a\u308a\u307e\u3059\u3002\u3053\u308c\u3092\u3001\u30e9\u30c3\u30d7\u3059\u308b NullObjectExtention \u30af\u30e9\u30b9\u3067\u306f\u3001public \u3067\u62e1\u5f35\u30e1\u30bd\u30c3\u30c9\u3092\u4f5c\u308b\u8a33\u3067\u3059\u3002<\/p>\n<p>\u30c6\u30b9\u30c8\u30b3\u30fc\u30c9\u306f\u3053\u3093\u306a\u611f\u3058\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nusing CheckNullObjectLib;\r\n\r\nnamespace CheckTestNullObject\r\n{\r\n    public partial class Form1 : Form\r\n    {\r\n        public Form1()\r\n        {\r\n            InitializeComponent();\r\n        }\r\n\r\n        private NullObject obj;\r\n\r\n        \/\/\/\r\n        \/\/\/ set button\r\n        \/\/\/\r\n        \/\/\/ &lt;param name=&amp;quot;sender&amp;quot; \/&gt;\r\n        \/\/\/ &lt;param name=&amp;quot;e&amp;quot; \/&gt;\r\n        private void button1_Click(object sender, EventArgs e)\r\n        {\r\n            obj = new NullObject();\r\n            obj.SetMessage(textBox1.Text);\r\n        }\r\n\r\n        \/\/\/\r\n        \/\/\/ get button\r\n        \/\/\/\r\n        \/\/\/ &lt;param name=&amp;quot;sender&amp;quot; \/&gt;\r\n        \/\/\/ &lt;param name=&amp;quot;e&amp;quot; \/&gt;\r\n        private void button2_Click(object sender, EventArgs e)\r\n        {\r\n            \/\/ \u3044\u304d\u306a\u308a set button \u3092\u62bc\u3057\u3066,\r\n            \/\/ obj \u304c null \u306e\u72b6\u614b\u3067\u3082\u5927\u4e08\u592b\r\n            textBox2.Text = obj.GetMessage();\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>\u5185\u90e8\u3067\u4fdd\u6301\u3057\u3066\u3044\u308b NullObject \u30aa\u30d6\u30b8\u30a7\u30af\u30c8 obj \u306f\u3001set button \u3092\u62bc\u3057\u305f\u3068\u304d\u306b new \u3059\u308b\u8a33\u3067\u3059\u304c\u3001\u9593\u9055\u3063\u3066 get button \u3092\u62bc\u3057\u305f\u3068\u3057\u3066\u3082 GetMessage \u30e1\u30bd\u30c3\u30c9\u3067\u4f8b\u5916\u304c\u767a\u751f\u3057\u307e\u305b\u3093\u3002\u306a\u3093\u3068\u4fbf\u5229\u306a\u30c3!!! objective-c \u306e\u6280\u304c\u30c3!!! \u3063\u3066\u601d\u3044\u307e\u3059\u304c\u3001\u307e\u3041\u3001\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u306e\u57fa\u790e\u3057\u3066\u306f\u300c\u304d\u3061\u3093\u3068 obj \u306f\u521d\u671f\u5316\u3057\u3066\u304a\u3053\u3046\u306d\u300d\u3068\u304b\u300cget button \u3092\u62bc\u3057\u305f\u6642\u306b null \u30c1\u30a7\u30c3\u30af\u3092\u3057\u3088\u3046\u306d\u300d\u3068\u3044\u3046\u306e\u304c C# \u306e\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u3068\u3057\u3066\u6b63\u3057\u3044\u3084\u308a\u65b9\u3067\u3059\u306d\u3002<\/p>\n<p>\u25a0\u3069\u3093\u306a\u3068\u3053\u308d\u3067\u4f7f\u3046\u306e\u304b\uff1f<\/p>\n<p>\u4f7f\u3044\u51e6\u3068\u3057\u3066\u306f\u3001null \u30c1\u30a7\u30c3\u30af\u3092\u7701\u304f\u3063\u3066\u306e\u3082\u305d\u3046\u306a\u306e\u3067\u3059\u304c\u3001\u30e1\u30bd\u30c3\u30c9\u30c1\u30a7\u30fc\u30f3\u306e\u3068\u3053\u308d\u3067\u9014\u4e2d\u306b null \u3092\u542b\u3081\u3089\u308c\u308b\u3068\u3044\u3046\u30e1\u30ea\u30c3\u30c8\u304c\u3042\u308a\u307e\u3059\u3002<br \/>\nLINQ \u306e\u5b9a\u756a\u3068\u3057\u3066\u3001<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar data = ...\r\nvar result = data.From( ... ).Where( ... ).Select( ... ).OrderBy( ... );\r\n<\/pre>\n<p>\u3068\u3044\u3046\u66f8\u304d\u65b9\u3092\u3057\u305f\u5834\u5408\u3001From, Where, Select \u30e1\u30bd\u30c3\u30c9 null \u3092\u8fd4\u3059\u3068\u4e0d\u610f\u306b\u843d\u3061\u3066\u3057\u307e\u3046\u8a33\u3067\u3059\u3002\u3072\u3068\u3064\u306e\u65b9\u6cd5\u3068\u3057\u3066\u306f\u3001\u305d\u308c\u305e\u308c\u306e\u30e1\u30bd\u30c3\u30c9\u304c null \u3092\u8fd4\u3055\u306a\u3044\u3088\u3046\u306b\u5b9f\u88c5\u3059\u308c\u3070\u3088\u3044\u306e\u3067\u3059\u3002\u3082\u3046\u3072\u3068\u3064\u306e\u65b9\u6cd5\u3068\u3057\u3066\u306f\u3001\u5148\u306e\u62e1\u5f35\u30e1\u30bd\u30c3\u30c9\u306e\u30c6\u30af\u30cb\u30c3\u30af\u3092\u4f7f\u3063\u3066 null \u3067\u843d\u3061\u306a\u3044\u3068\u3044\u3046\u65b9\u6cd5\u304c\u4f7f\u3048\u307e\u3059\u3002<\/p>\n<p>\u307e\u3042\u3001\u5909\u6570\u3092\u6562\u3048\u3066 null \u3067\u8fd4\u3057\u305f\u3044\u3068\u304d\u306b\u4f7f\u3048\u308b\u304b\u306a\u3041\u3068\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>WCF\u30b5\u30fc\u30d3\u30b9\u3092\u8abf\u3079\u3066\u3044\u308b\u3068\u304d\u306b\u898b\u3064\u3051\u305f\u306e\u3067\u3001\u3061\u3087\u3063\u3068\u30e1\u30e2\u7684\u306b\u3002\u4e0b\u8a18\u306e SafeInvoke \u30e1\u30bd\u30c3\u30c9\u306e\u3068\u3053\u308d\u3067\u3059\u3002 Method call if not null in C# &#8211; Stack Overflo &hellip; <a href=\"https:\/\/www.moonmile.net\/blog\/archives\/3390\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[23],"tags":[],"class_list":["post-3390","post","type-post","status-publish","format-standard","hentry","category-csharp"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/3390","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/comments?post=3390"}],"version-history":[{"count":5,"href":"https:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/3390\/revisions"}],"predecessor-version":[{"id":3399,"href":"https:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/3390\/revisions\/3399"}],"wp:attachment":[{"href":"https:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/media?parent=3390"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/categories?post=3390"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/tags?post=3390"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}