[{"data":1,"prerenderedAt":433},["ShallowReactive",2],{"blog:2006:handlingnullablevaluetypesindotnet":3,"blogMore-Development":419,"comments-handlingnullablevaluetypesindotnet":432},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"date":10,"category":11,"tags":12,"excerpt":15,"body":30,"_type":410,"_id":411,"_source":412,"_file":413,"_stem":414,"_extension":415,"url":416,"wordCount":417,"minutes":89,"commentCount":418},"/blog/2006/handlingnullablevaluetypesindotnet","2006",false,"en","Handling nullable value types in .NET 1.x","One of the great new features in .NET 2.0 is support for nullable types, especially important when dealing with value types such as Int or DateTime values coming from a database. Previously you were forced to either set and track another binary flag or to use a “special” value to represent null that sooner or later would turn up in real data.","2006-04-20T01:53:00+00:00","Development",[13,14],".NET","C#",{"type":16,"children":17},"root",[18,25],{"type":19,"tag":20,"props":21,"children":22},"element","p",{},[23],{"type":24,"value":9},"text",{"type":19,"tag":20,"props":26,"children":27},{},[28],{"type":24,"value":29},"In C# 2.0 thanks to a bit of syntactic sugar we can do this;",{"type":16,"children":31,"toc":408},[32,36,40,130,135,267,280,296,388,393,402],{"type":19,"tag":20,"props":33,"children":34},{},[35],{"type":24,"value":9},{"type":19,"tag":20,"props":37,"children":38},{},[39],{"type":24,"value":29},{"type":19,"tag":41,"props":42,"children":47},"pre",{"className":43,"code":44,"language":45,"meta":46,"style":46},"language-csharp shiki shiki-themes everforest-light dracula","int? nullable1 = 1;\nif (nullable1 == null) nullable1 = 2;\n","csharp","",[48],{"type":19,"tag":49,"props":50,"children":51},"code",{"__ignoreMap":46},[52,87],{"type":19,"tag":53,"props":54,"children":57},"span",{"class":55,"line":56},"line",1,[58,64,70,76,82],{"type":19,"tag":53,"props":59,"children":61},{"style":60},"--shiki-default:#3A94C5;--shiki-dark:#FF79C6",[62],{"type":24,"value":63},"int",{"type":19,"tag":53,"props":65,"children":67},{"style":66},"--shiki-default:#5C6A72;--shiki-dark:#F8F8F2",[68],{"type":24,"value":69},"? nullable1 ",{"type":19,"tag":53,"props":71,"children":73},{"style":72},"--shiki-default:#F57D26;--shiki-dark:#FF79C6",[74],{"type":24,"value":75},"=",{"type":19,"tag":53,"props":77,"children":79},{"style":78},"--shiki-default:#DF69BA;--shiki-dark:#BD93F9",[80],{"type":24,"value":81}," 1",{"type":19,"tag":53,"props":83,"children":84},{"style":66},[85],{"type":24,"value":86},";\n",{"type":19,"tag":53,"props":88,"children":90},{"class":55,"line":89},2,[91,97,102,107,112,117,121,126],{"type":19,"tag":53,"props":92,"children":94},{"style":93},"--shiki-default:#F85552;--shiki-dark:#FF79C6",[95],{"type":24,"value":96},"if",{"type":19,"tag":53,"props":98,"children":99},{"style":66},[100],{"type":24,"value":101}," (nullable1 ",{"type":19,"tag":53,"props":103,"children":104},{"style":72},[105],{"type":24,"value":106},"==",{"type":19,"tag":53,"props":108,"children":109},{"style":78},[110],{"type":24,"value":111}," null",{"type":19,"tag":53,"props":113,"children":114},{"style":66},[115],{"type":24,"value":116},") nullable1 ",{"type":19,"tag":53,"props":118,"children":119},{"style":72},[120],{"type":24,"value":75},{"type":19,"tag":53,"props":122,"children":123},{"style":78},[124],{"type":24,"value":125}," 2",{"type":19,"tag":53,"props":127,"children":128},{"style":66},[129],{"type":24,"value":86},{"type":19,"tag":20,"props":131,"children":132},{},[133],{"type":24,"value":134},"Which the C# compiler actually turns into the following:",{"type":19,"tag":41,"props":136,"children":138},{"className":43,"code":137,"language":45,"meta":46,"style":46},"Nullable\u003Cint> nullable1 = new Nullable\u003Cint>(1);\nif (!nullable1.HasValue) nullable1 = new Nullable\u003Cint>(2);\n",[139],{"type":19,"tag":49,"props":140,"children":141},{"__ignoreMap":46},[142,202],{"type":19,"tag":53,"props":143,"children":144},{"class":55,"line":56},[145,151,156,160,165,169,174,179,183,187,192,197],{"type":19,"tag":53,"props":146,"children":148},{"style":147},"--shiki-default:#3A94C5;--shiki-default-font-style:inherit;--shiki-dark:#8BE9FD;--shiki-dark-font-style:italic",[149],{"type":24,"value":150},"Nullable",{"type":19,"tag":53,"props":152,"children":153},{"style":66},[154],{"type":24,"value":155},"\u003C",{"type":19,"tag":53,"props":157,"children":158},{"style":60},[159],{"type":24,"value":63},{"type":19,"tag":53,"props":161,"children":162},{"style":66},[163],{"type":24,"value":164},"> nullable1 ",{"type":19,"tag":53,"props":166,"children":167},{"style":72},[168],{"type":24,"value":75},{"type":19,"tag":53,"props":170,"children":171},{"style":93},[172],{"type":24,"value":173}," new",{"type":19,"tag":53,"props":175,"children":176},{"style":147},[177],{"type":24,"value":178}," Nullable",{"type":19,"tag":53,"props":180,"children":181},{"style":66},[182],{"type":24,"value":155},{"type":19,"tag":53,"props":184,"children":185},{"style":60},[186],{"type":24,"value":63},{"type":19,"tag":53,"props":188,"children":189},{"style":66},[190],{"type":24,"value":191},">(",{"type":19,"tag":53,"props":193,"children":194},{"style":78},[195],{"type":24,"value":196},"1",{"type":19,"tag":53,"props":198,"children":199},{"style":66},[200],{"type":24,"value":201},");\n",{"type":19,"tag":53,"props":203,"children":204},{"class":55,"line":89},[205,209,214,219,224,230,234,238,242,246,250,254,258,263],{"type":19,"tag":53,"props":206,"children":207},{"style":93},[208],{"type":24,"value":96},{"type":19,"tag":53,"props":210,"children":211},{"style":66},[212],{"type":24,"value":213}," (",{"type":19,"tag":53,"props":215,"children":216},{"style":72},[217],{"type":24,"value":218},"!",{"type":19,"tag":53,"props":220,"children":221},{"style":66},[222],{"type":24,"value":223},"nullable1.",{"type":19,"tag":53,"props":225,"children":227},{"style":226},"--shiki-default:#35A77C;--shiki-dark:#F8F8F2",[228],{"type":24,"value":229},"HasValue",{"type":19,"tag":53,"props":231,"children":232},{"style":66},[233],{"type":24,"value":116},{"type":19,"tag":53,"props":235,"children":236},{"style":72},[237],{"type":24,"value":75},{"type":19,"tag":53,"props":239,"children":240},{"style":93},[241],{"type":24,"value":173},{"type":19,"tag":53,"props":243,"children":244},{"style":147},[245],{"type":24,"value":178},{"type":19,"tag":53,"props":247,"children":248},{"style":66},[249],{"type":24,"value":155},{"type":19,"tag":53,"props":251,"children":252},{"style":60},[253],{"type":24,"value":63},{"type":19,"tag":53,"props":255,"children":256},{"style":66},[257],{"type":24,"value":191},{"type":19,"tag":53,"props":259,"children":260},{"style":78},[261],{"type":24,"value":262},"2",{"type":19,"tag":53,"props":264,"children":265},{"style":66},[266],{"type":24,"value":201},{"type":19,"tag":20,"props":268,"children":269},{},[270,272,278],{"type":24,"value":271},"This makes use of the new ",{"type":19,"tag":49,"props":273,"children":275},{"className":274},[],[276],{"type":24,"value":277},"Nullable\u003CT>",{"type":24,"value":279}," generic structure to wrap the value with an additional boolean HasValue property.",{"type":19,"tag":20,"props":281,"children":282},{},[283,285,294],{"type":24,"value":284},"That looks surprisingly similar to the syntax of the open-source ",{"type":19,"tag":286,"props":287,"children":291},"a",{"href":288,"rel":289},"https://nullabletypes.sourceforge.net",[290],"nofollow",[292],{"type":24,"value":293},"NullableTypes",{"type":24,"value":295}," project you can use with .NET 1.1 (or 2.0):",{"type":19,"tag":41,"props":297,"children":299},{"className":43,"code":298,"language":45,"meta":46,"style":46},"NullableInt32 nullable1 = new NullableInt32(1);\nif (nullable1.IsNull) nullable1 = new NullableInt(2);\n",[300],{"type":19,"tag":49,"props":301,"children":302},{"__ignoreMap":46},[303,342],{"type":19,"tag":53,"props":304,"children":305},{"class":55,"line":56},[306,311,316,320,324,329,334,338],{"type":19,"tag":53,"props":307,"children":308},{"style":147},[309],{"type":24,"value":310},"NullableInt32",{"type":19,"tag":53,"props":312,"children":313},{"style":66},[314],{"type":24,"value":315}," nullable1 ",{"type":19,"tag":53,"props":317,"children":318},{"style":72},[319],{"type":24,"value":75},{"type":19,"tag":53,"props":321,"children":322},{"style":93},[323],{"type":24,"value":173},{"type":19,"tag":53,"props":325,"children":326},{"style":147},[327],{"type":24,"value":328}," NullableInt32",{"type":19,"tag":53,"props":330,"children":331},{"style":66},[332],{"type":24,"value":333},"(",{"type":19,"tag":53,"props":335,"children":336},{"style":78},[337],{"type":24,"value":196},{"type":19,"tag":53,"props":339,"children":340},{"style":66},[341],{"type":24,"value":201},{"type":19,"tag":53,"props":343,"children":344},{"class":55,"line":89},[345,349,354,359,363,367,371,376,380,384],{"type":19,"tag":53,"props":346,"children":347},{"style":93},[348],{"type":24,"value":96},{"type":19,"tag":53,"props":350,"children":351},{"style":66},[352],{"type":24,"value":353}," (nullable1.",{"type":19,"tag":53,"props":355,"children":356},{"style":226},[357],{"type":24,"value":358},"IsNull",{"type":19,"tag":53,"props":360,"children":361},{"style":66},[362],{"type":24,"value":116},{"type":19,"tag":53,"props":364,"children":365},{"style":72},[366],{"type":24,"value":75},{"type":19,"tag":53,"props":368,"children":369},{"style":93},[370],{"type":24,"value":173},{"type":19,"tag":53,"props":372,"children":373},{"style":147},[374],{"type":24,"value":375}," NullableInt",{"type":19,"tag":53,"props":377,"children":378},{"style":66},[379],{"type":24,"value":333},{"type":19,"tag":53,"props":381,"children":382},{"style":78},[383],{"type":24,"value":262},{"type":19,"tag":53,"props":385,"children":386},{"style":66},[387],{"type":24,"value":201},{"type":19,"tag":20,"props":389,"children":390},{},[391],{"type":24,"value":392},"Disclaimer: I’ve worked on the NullableTypes project specifically implementing the IXmlSerializable, NullableGuid & NullableTimeSpan.",{"type":19,"tag":20,"props":394,"children":395},{},[396],{"type":19,"tag":397,"props":398,"children":399},"em",{},[400],{"type":24,"value":401},"[)amien",{"type":19,"tag":403,"props":404,"children":405},"style",{},[406],{"type":24,"value":407},"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":46,"searchDepth":89,"depth":89,"links":409},[],"markdown","content:blog:2006:handlingnullablevaluetypesindotnet.md","content","blog/2006/handlingnullablevaluetypesindotnet.md","blog/2006/handlingnullablevaluetypesindotnet","md","/blog/2006/handlingnullablevaluetypesindotnet/",236,0,[420,424,428],{"title":421,"date":422,"url":423},"HTML5 Video Cheatsheet: Optimizing videos for the web","2025-12-05T00:00:00Z","/blog/2025/html5-video-cheatsheet/",{"title":425,"date":426,"url":427},"Transactions in the MongoDB EF Core Provider","2025-10-25","/blog/2025/mongodb-explicit-transactions/",{"title":429,"date":430,"url":431},"Queryable Encryption with the MongoDB EF Core Provider","2025-09-22","/blog/2025/mongodb-queryable-encryption/",[],1779264597545]