[{"data":1,"prerenderedAt":827},["ShallowReactive",2],{"blog:2005:conditionaloperatorbuginnet1andnet2":3,"blogMore-Development":813,"comments-conditionaloperatorbuginnet1andnet2":826},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"date":10,"category":11,"tags":12,"excerpt":16,"body":31,"_type":804,"_id":805,"_source":806,"_file":807,"_stem":808,"_extension":809,"url":810,"wordCount":811,"minutes":198,"commentCount":812},"/blog/2005/conditionaloperatorbuginnet1andnet2","2005",false,"en","Conditional operator bug in .NET 1.x & 2.0","Update: This behavior is as expected according to the .NET team but it's probably unexpected for most users.","2005-09-18T11:14:00+00:00","Development",[13,14,15],".NET","debugging","C#",{"type":17,"children":18},"root",[19,26],{"type":20,"tag":21,"props":22,"children":23},"element","p",{},[24],{"type":25,"value":9},"text",{"type":20,"tag":21,"props":27,"children":28},{},[29],{"type":25,"value":30},"I encountered a strange problem this week when a conditional operator appeared to be evaluating the false expression contrary to the C# documentation. The line looked like:",{"type":17,"children":32,"toc":802},[33,37,41,113,118,123,128,133,138,265,270,787,796],{"type":20,"tag":21,"props":34,"children":35},{},[36],{"type":25,"value":9},{"type":20,"tag":21,"props":38,"children":39},{},[40],{"type":25,"value":30},{"type":20,"tag":42,"props":43,"children":48},"pre",{"className":44,"code":45,"language":46,"meta":47,"style":47},"language-csharp shiki shiki-themes everforest-light dracula","return testObject == null ? null : testObject.InstanceVariable;\n","csharp","",[49],{"type":20,"tag":50,"props":51,"children":52},"code",{"__ignoreMap":47},[53],{"type":20,"tag":54,"props":55,"children":58},"span",{"class":56,"line":57},"line",1,[59,65,71,77,83,88,92,97,102,108],{"type":20,"tag":54,"props":60,"children":62},{"style":61},"--shiki-default:#F85552;--shiki-dark:#FF79C6",[63],{"type":25,"value":64},"return",{"type":20,"tag":54,"props":66,"children":68},{"style":67},"--shiki-default:#5C6A72;--shiki-dark:#F8F8F2",[69],{"type":25,"value":70}," testObject ",{"type":20,"tag":54,"props":72,"children":74},{"style":73},"--shiki-default:#F57D26;--shiki-dark:#FF79C6",[75],{"type":25,"value":76},"==",{"type":20,"tag":54,"props":78,"children":80},{"style":79},"--shiki-default:#DF69BA;--shiki-dark:#BD93F9",[81],{"type":25,"value":82}," null",{"type":20,"tag":54,"props":84,"children":85},{"style":73},[86],{"type":25,"value":87}," ?",{"type":20,"tag":54,"props":89,"children":90},{"style":79},[91],{"type":25,"value":82},{"type":20,"tag":54,"props":93,"children":94},{"style":73},[95],{"type":25,"value":96}," :",{"type":20,"tag":54,"props":98,"children":99},{"style":67},[100],{"type":25,"value":101}," testObject.",{"type":20,"tag":54,"props":103,"children":105},{"style":104},"--shiki-default:#35A77C;--shiki-dark:#F8F8F2",[106],{"type":25,"value":107},"InstanceVariable",{"type":20,"tag":54,"props":109,"children":110},{"style":67},[111],{"type":25,"value":112},";\n",{"type":20,"tag":21,"props":114,"children":115},{},[116],{"type":25,"value":117},"The point of this line is to prevent accessing .InstanceVariable if the object is null and yet every time this line executed a NullReferenceException is thrown (and no I wasn’t overloading the == operator).",{"type":20,"tag":21,"props":119,"children":120},{},[121],{"type":25,"value":122},"With a little experimentation I was able to narrow it down and produce a simple test case that exercised the bug on .NET 1.1 and .NET 2.0.",{"type":20,"tag":21,"props":124,"children":125},{},[126],{"type":25,"value":127},"The problem is this: If the return parts are not of the same type and one of the members supports implicit conversion to the expect type, then it is called regardless of whether it is the the true part or not.",{"type":20,"tag":21,"props":129,"children":130},{},[131],{"type":25,"value":132},"I filed the bug with Microsoft and it was confirmed last night by one of their C# engineers, there is a small chance it will be fixed in .NET 2.0 before release but we’ll have to see. I checked out some of the .NET classes and they too always access the object during an implicit conversion, however they are all value types which can not be null.",{"type":20,"tag":21,"props":134,"children":135},{},[136],{"type":25,"value":137},"In the mean time there is an easy way to avoid this by modifying your implicit conversion methods to check for null before converting.",{"type":20,"tag":42,"props":139,"children":141},{"className":44,"code":140,"language":46,"meta":47,"style":47},"public static implicit operator string(ClassWithImplicitConversion objToConvert) {\n    return objToConvert == null ? string.Empty : objToConvert.ToString();\n}\n",[142],{"type":20,"tag":50,"props":143,"children":144},{"__ignoreMap":47},[145,196,256],{"type":20,"tag":54,"props":146,"children":147},{"class":56,"line":57},[148,153,158,163,169,175,180,185,191],{"type":20,"tag":54,"props":149,"children":150},{"style":73},[151],{"type":25,"value":152},"public",{"type":20,"tag":54,"props":154,"children":155},{"style":73},[156],{"type":25,"value":157}," static",{"type":20,"tag":54,"props":159,"children":160},{"style":67},[161],{"type":25,"value":162}," implicit ",{"type":20,"tag":54,"props":164,"children":166},{"style":165},"--shiki-default:#3A94C5;--shiki-default-font-style:inherit;--shiki-dark:#8BE9FD;--shiki-dark-font-style:italic",[167],{"type":25,"value":168},"operator",{"type":20,"tag":54,"props":170,"children":172},{"style":171},"--shiki-default:#8DA101;--shiki-dark:#50FA7B",[173],{"type":25,"value":174}," string",{"type":20,"tag":54,"props":176,"children":177},{"style":67},[178],{"type":25,"value":179},"(",{"type":20,"tag":54,"props":181,"children":182},{"style":165},[183],{"type":25,"value":184},"ClassWithImplicitConversion",{"type":20,"tag":54,"props":186,"children":188},{"style":187},"--shiki-default:#5C6A72;--shiki-default-font-style:inherit;--shiki-dark:#FFB86C;--shiki-dark-font-style:italic",[189],{"type":25,"value":190}," objToConvert",{"type":20,"tag":54,"props":192,"children":193},{"style":67},[194],{"type":25,"value":195},") {\n",{"type":20,"tag":54,"props":197,"children":199},{"class":56,"line":198},2,[200,205,210,214,218,222,227,232,237,241,246,251],{"type":20,"tag":54,"props":201,"children":202},{"style":61},[203],{"type":25,"value":204},"    return",{"type":20,"tag":54,"props":206,"children":207},{"style":67},[208],{"type":25,"value":209}," objToConvert ",{"type":20,"tag":54,"props":211,"children":212},{"style":73},[213],{"type":25,"value":76},{"type":20,"tag":54,"props":215,"children":216},{"style":79},[217],{"type":25,"value":82},{"type":20,"tag":54,"props":219,"children":220},{"style":73},[221],{"type":25,"value":87},{"type":20,"tag":54,"props":223,"children":225},{"style":224},"--shiki-default:#3A94C5;--shiki-dark:#FF79C6",[226],{"type":25,"value":174},{"type":20,"tag":54,"props":228,"children":229},{"style":67},[230],{"type":25,"value":231},".",{"type":20,"tag":54,"props":233,"children":234},{"style":104},[235],{"type":25,"value":236},"Empty",{"type":20,"tag":54,"props":238,"children":239},{"style":73},[240],{"type":25,"value":96},{"type":20,"tag":54,"props":242,"children":243},{"style":67},[244],{"type":25,"value":245}," objToConvert.",{"type":20,"tag":54,"props":247,"children":248},{"style":171},[249],{"type":25,"value":250},"ToString",{"type":20,"tag":54,"props":252,"children":253},{"style":67},[254],{"type":25,"value":255},"();\n",{"type":20,"tag":54,"props":257,"children":259},{"class":56,"line":258},3,[260],{"type":20,"tag":54,"props":261,"children":262},{"style":67},[263],{"type":25,"value":264},"}\n",{"type":20,"tag":21,"props":266,"children":267},{},[268],{"type":25,"value":269},"A full test case appears below:",{"type":20,"tag":42,"props":271,"children":273},{"className":44,"code":272,"language":46,"meta":47,"style":47},"using System;\n\nnamespace ProveConditionalBug {\n     class Program {\n         static void Main(string[] args) {\n             Console.Out.WriteLine(\"Testing... \" + ProveConditional());\n         }\n\n        static string ProveConditional() {\n            ClassHoldingImplicitConversionMember testObject = null;\n            return (testObject == null) ? null : testObject.InstanceVariable;\n        }\n     }\n\n    class ClassHoldingImplicitConversionMember {\n        public ClassWithImplicitConversion InstanceVariable = null;\u003Cbr />     }\n\n    class ClassWithImplicitConversion {\n         public string Value = \"Test\";\n         public static implicit operator string(ClassWithImplicitConversion objToConvert) {\n             return objToConvert.Value;\n         }\n     }\n}\n",[274],{"type":20,"tag":50,"props":275,"children":276},{"__ignoreMap":47},[277,295,304,323,342,384,442,451,459,481,507,559,568,577,585,603,635,643,659,699,741,763,771,779],{"type":20,"tag":54,"props":278,"children":279},{"class":56,"line":57},[280,285,291],{"type":20,"tag":54,"props":281,"children":282},{"style":61},[283],{"type":25,"value":284},"using",{"type":20,"tag":54,"props":286,"children":288},{"style":287},"--shiki-default:#DF69BA;--shiki-default-font-style:inherit;--shiki-dark:#8BE9FD;--shiki-dark-font-style:italic",[289],{"type":25,"value":290}," System",{"type":20,"tag":54,"props":292,"children":293},{"style":67},[294],{"type":25,"value":112},{"type":20,"tag":54,"props":296,"children":297},{"class":56,"line":198},[298],{"type":20,"tag":54,"props":299,"children":301},{"emptyLinePlaceholder":300},true,[302],{"type":25,"value":303},"\n",{"type":20,"tag":54,"props":305,"children":306},{"class":56,"line":258},[307,313,318],{"type":20,"tag":54,"props":308,"children":310},{"style":309},"--shiki-default:#35A77C;--shiki-dark:#FF79C6",[311],{"type":25,"value":312},"namespace",{"type":20,"tag":54,"props":314,"children":315},{"style":287},[316],{"type":25,"value":317}," ProveConditionalBug",{"type":20,"tag":54,"props":319,"children":320},{"style":67},[321],{"type":25,"value":322}," {\n",{"type":20,"tag":54,"props":324,"children":326},{"class":56,"line":325},4,[327,332,338],{"type":20,"tag":54,"props":328,"children":329},{"style":61},[330],{"type":25,"value":331},"     class",{"type":20,"tag":54,"props":333,"children":335},{"style":334},"--shiki-default:#3A94C5;--shiki-dark:#8BE9FD",[336],{"type":25,"value":337}," Program",{"type":20,"tag":54,"props":339,"children":340},{"style":67},[341],{"type":25,"value":322},{"type":20,"tag":54,"props":343,"children":345},{"class":56,"line":344},5,[346,351,356,361,365,370,375,380],{"type":20,"tag":54,"props":347,"children":348},{"style":73},[349],{"type":25,"value":350},"         static",{"type":20,"tag":54,"props":352,"children":353},{"style":224},[354],{"type":25,"value":355}," void",{"type":20,"tag":54,"props":357,"children":358},{"style":171},[359],{"type":25,"value":360}," Main",{"type":20,"tag":54,"props":362,"children":363},{"style":67},[364],{"type":25,"value":179},{"type":20,"tag":54,"props":366,"children":367},{"style":224},[368],{"type":25,"value":369},"string",{"type":20,"tag":54,"props":371,"children":372},{"style":67},[373],{"type":25,"value":374},"[] ",{"type":20,"tag":54,"props":376,"children":377},{"style":187},[378],{"type":25,"value":379},"args",{"type":20,"tag":54,"props":381,"children":382},{"style":67},[383],{"type":25,"value":195},{"type":20,"tag":54,"props":385,"children":387},{"class":56,"line":386},6,[388,393,398,402,407,411,417,423,427,432,437],{"type":20,"tag":54,"props":389,"children":390},{"style":67},[391],{"type":25,"value":392},"             Console.",{"type":20,"tag":54,"props":394,"children":395},{"style":104},[396],{"type":25,"value":397},"Out",{"type":20,"tag":54,"props":399,"children":400},{"style":67},[401],{"type":25,"value":231},{"type":20,"tag":54,"props":403,"children":404},{"style":171},[405],{"type":25,"value":406},"WriteLine",{"type":20,"tag":54,"props":408,"children":409},{"style":67},[410],{"type":25,"value":179},{"type":20,"tag":54,"props":412,"children":414},{"style":413},"--shiki-default:#8DA101;--shiki-dark:#E9F284",[415],{"type":25,"value":416},"\"",{"type":20,"tag":54,"props":418,"children":420},{"style":419},"--shiki-default:#8DA101;--shiki-dark:#F1FA8C",[421],{"type":25,"value":422},"Testing... ",{"type":20,"tag":54,"props":424,"children":425},{"style":413},[426],{"type":25,"value":416},{"type":20,"tag":54,"props":428,"children":429},{"style":73},[430],{"type":25,"value":431}," +",{"type":20,"tag":54,"props":433,"children":434},{"style":171},[435],{"type":25,"value":436}," ProveConditional",{"type":20,"tag":54,"props":438,"children":439},{"style":67},[440],{"type":25,"value":441},"());\n",{"type":20,"tag":54,"props":443,"children":445},{"class":56,"line":444},7,[446],{"type":20,"tag":54,"props":447,"children":448},{"style":67},[449],{"type":25,"value":450},"         }\n",{"type":20,"tag":54,"props":452,"children":454},{"class":56,"line":453},8,[455],{"type":20,"tag":54,"props":456,"children":457},{"emptyLinePlaceholder":300},[458],{"type":25,"value":303},{"type":20,"tag":54,"props":460,"children":462},{"class":56,"line":461},9,[463,468,472,476],{"type":20,"tag":54,"props":464,"children":465},{"style":73},[466],{"type":25,"value":467},"        static",{"type":20,"tag":54,"props":469,"children":470},{"style":224},[471],{"type":25,"value":174},{"type":20,"tag":54,"props":473,"children":474},{"style":171},[475],{"type":25,"value":436},{"type":20,"tag":54,"props":477,"children":478},{"style":67},[479],{"type":25,"value":480},"() {\n",{"type":20,"tag":54,"props":482,"children":484},{"class":56,"line":483},10,[485,490,494,499,503],{"type":20,"tag":54,"props":486,"children":487},{"style":165},[488],{"type":25,"value":489},"            ClassHoldingImplicitConversionMember",{"type":20,"tag":54,"props":491,"children":492},{"style":67},[493],{"type":25,"value":70},{"type":20,"tag":54,"props":495,"children":496},{"style":73},[497],{"type":25,"value":498},"=",{"type":20,"tag":54,"props":500,"children":501},{"style":79},[502],{"type":25,"value":82},{"type":20,"tag":54,"props":504,"children":505},{"style":67},[506],{"type":25,"value":112},{"type":20,"tag":54,"props":508,"children":510},{"class":56,"line":509},11,[511,516,521,525,529,534,539,543,547,551,555],{"type":20,"tag":54,"props":512,"children":513},{"style":61},[514],{"type":25,"value":515},"            return",{"type":20,"tag":54,"props":517,"children":518},{"style":67},[519],{"type":25,"value":520}," (testObject ",{"type":20,"tag":54,"props":522,"children":523},{"style":73},[524],{"type":25,"value":76},{"type":20,"tag":54,"props":526,"children":527},{"style":79},[528],{"type":25,"value":82},{"type":20,"tag":54,"props":530,"children":531},{"style":67},[532],{"type":25,"value":533},") ",{"type":20,"tag":54,"props":535,"children":536},{"style":73},[537],{"type":25,"value":538},"?",{"type":20,"tag":54,"props":540,"children":541},{"style":79},[542],{"type":25,"value":82},{"type":20,"tag":54,"props":544,"children":545},{"style":73},[546],{"type":25,"value":96},{"type":20,"tag":54,"props":548,"children":549},{"style":67},[550],{"type":25,"value":101},{"type":20,"tag":54,"props":552,"children":553},{"style":104},[554],{"type":25,"value":107},{"type":20,"tag":54,"props":556,"children":557},{"style":67},[558],{"type":25,"value":112},{"type":20,"tag":54,"props":560,"children":562},{"class":56,"line":561},12,[563],{"type":20,"tag":54,"props":564,"children":565},{"style":67},[566],{"type":25,"value":567},"        }\n",{"type":20,"tag":54,"props":569,"children":571},{"class":56,"line":570},13,[572],{"type":20,"tag":54,"props":573,"children":574},{"style":67},[575],{"type":25,"value":576},"     }\n",{"type":20,"tag":54,"props":578,"children":580},{"class":56,"line":579},14,[581],{"type":20,"tag":54,"props":582,"children":583},{"emptyLinePlaceholder":300},[584],{"type":25,"value":303},{"type":20,"tag":54,"props":586,"children":588},{"class":56,"line":587},15,[589,594,599],{"type":20,"tag":54,"props":590,"children":591},{"style":61},[592],{"type":25,"value":593},"    class",{"type":20,"tag":54,"props":595,"children":596},{"style":334},[597],{"type":25,"value":598}," ClassHoldingImplicitConversionMember",{"type":20,"tag":54,"props":600,"children":601},{"style":67},[602],{"type":25,"value":322},{"type":20,"tag":54,"props":604,"children":606},{"class":56,"line":605},16,[607,612,617,622,626,630],{"type":20,"tag":54,"props":608,"children":609},{"style":73},[610],{"type":25,"value":611},"        public",{"type":20,"tag":54,"props":613,"children":614},{"style":165},[615],{"type":25,"value":616}," ClassWithImplicitConversion",{"type":20,"tag":54,"props":618,"children":619},{"style":67},[620],{"type":25,"value":621}," InstanceVariable ",{"type":20,"tag":54,"props":623,"children":624},{"style":73},[625],{"type":25,"value":498},{"type":20,"tag":54,"props":627,"children":628},{"style":79},[629],{"type":25,"value":82},{"type":20,"tag":54,"props":631,"children":632},{"style":67},[633],{"type":25,"value":634},";\u003Cbr />     }\n",{"type":20,"tag":54,"props":636,"children":638},{"class":56,"line":637},17,[639],{"type":20,"tag":54,"props":640,"children":641},{"emptyLinePlaceholder":300},[642],{"type":25,"value":303},{"type":20,"tag":54,"props":644,"children":646},{"class":56,"line":645},18,[647,651,655],{"type":20,"tag":54,"props":648,"children":649},{"style":61},[650],{"type":25,"value":593},{"type":20,"tag":54,"props":652,"children":653},{"style":334},[654],{"type":25,"value":616},{"type":20,"tag":54,"props":656,"children":657},{"style":67},[658],{"type":25,"value":322},{"type":20,"tag":54,"props":660,"children":662},{"class":56,"line":661},19,[663,668,672,677,681,686,691,695],{"type":20,"tag":54,"props":664,"children":665},{"style":73},[666],{"type":25,"value":667},"         public",{"type":20,"tag":54,"props":669,"children":670},{"style":224},[671],{"type":25,"value":174},{"type":20,"tag":54,"props":673,"children":674},{"style":67},[675],{"type":25,"value":676}," Value ",{"type":20,"tag":54,"props":678,"children":679},{"style":73},[680],{"type":25,"value":498},{"type":20,"tag":54,"props":682,"children":683},{"style":413},[684],{"type":25,"value":685}," \"",{"type":20,"tag":54,"props":687,"children":688},{"style":419},[689],{"type":25,"value":690},"Test",{"type":20,"tag":54,"props":692,"children":693},{"style":413},[694],{"type":25,"value":416},{"type":20,"tag":54,"props":696,"children":697},{"style":67},[698],{"type":25,"value":112},{"type":20,"tag":54,"props":700,"children":702},{"class":56,"line":701},20,[703,707,711,716,721,725,729,733,737],{"type":20,"tag":54,"props":704,"children":705},{"style":73},[706],{"type":25,"value":667},{"type":20,"tag":54,"props":708,"children":709},{"style":73},[710],{"type":25,"value":157},{"type":20,"tag":54,"props":712,"children":713},{"style":73},[714],{"type":25,"value":715}," implicit",{"type":20,"tag":54,"props":717,"children":718},{"style":224},[719],{"type":25,"value":720}," operator",{"type":20,"tag":54,"props":722,"children":723},{"style":224},[724],{"type":25,"value":174},{"type":20,"tag":54,"props":726,"children":727},{"style":67},[728],{"type":25,"value":179},{"type":20,"tag":54,"props":730,"children":731},{"style":165},[732],{"type":25,"value":184},{"type":20,"tag":54,"props":734,"children":735},{"style":187},[736],{"type":25,"value":190},{"type":20,"tag":54,"props":738,"children":739},{"style":67},[740],{"type":25,"value":195},{"type":20,"tag":54,"props":742,"children":744},{"class":56,"line":743},21,[745,750,754,759],{"type":20,"tag":54,"props":746,"children":747},{"style":61},[748],{"type":25,"value":749},"             return",{"type":20,"tag":54,"props":751,"children":752},{"style":67},[753],{"type":25,"value":245},{"type":20,"tag":54,"props":755,"children":756},{"style":104},[757],{"type":25,"value":758},"Value",{"type":20,"tag":54,"props":760,"children":761},{"style":67},[762],{"type":25,"value":112},{"type":20,"tag":54,"props":764,"children":766},{"class":56,"line":765},22,[767],{"type":20,"tag":54,"props":768,"children":769},{"style":67},[770],{"type":25,"value":450},{"type":20,"tag":54,"props":772,"children":774},{"class":56,"line":773},23,[775],{"type":20,"tag":54,"props":776,"children":777},{"style":67},[778],{"type":25,"value":576},{"type":20,"tag":54,"props":780,"children":782},{"class":56,"line":781},24,[783],{"type":20,"tag":54,"props":784,"children":785},{"style":67},[786],{"type":25,"value":264},{"type":20,"tag":21,"props":788,"children":789},{},[790],{"type":20,"tag":791,"props":792,"children":793},"em",{},[794],{"type":25,"value":795},"[)amien",{"type":20,"tag":797,"props":798,"children":799},"style",{},[800],{"type":25,"value":801},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":47,"searchDepth":198,"depth":198,"links":803},[],"markdown","content:blog:2005:conditionaloperatorbuginnet1andnet2.md","content","blog/2005/conditionaloperatorbuginnet1andnet2.md","blog/2005/conditionaloperatorbuginnet1andnet2","md","/blog/2005/conditionaloperatorbuginnet1andnet2/",409,0,[814,818,822],{"title":815,"date":816,"url":817},"HTML5 Video Cheatsheet: Optimizing videos for the web","2025-12-05T00:00:00Z","/blog/2025/html5-video-cheatsheet/",{"title":819,"date":820,"url":821},"Transactions in the MongoDB EF Core Provider","2025-10-25","/blog/2025/mongodb-explicit-transactions/",{"title":823,"date":824,"url":825},"Queryable Encryption with the MongoDB EF Core Provider","2025-09-22","/blog/2025/mongodb-queryable-encryption/",[],1779264598248]