[{"data":1,"prerenderedAt":602},["ShallowReactive",2],{"blog:2007:object-initializers-in-net-35":3,"blogMore-Development":588,"comments-object-initializers-in-net-35":601},{"_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":579,"_id":580,"_source":581,"_file":582,"_stem":583,"_extension":584,"url":585,"wordCount":586,"minutes":85,"commentCount":587},"/blog/2007/object-initializers-in-net-35","2007",false,"en","Object Initializers in .NET 3.5","One compiler improvement in .NET 3.5 is the object initializers feature that lets you concisely set properties of an object as you create it.","2007-11-04T01:17:53+00:00","Development",[13,14,15],".NET","C#","VB.NET",{"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},"If you’ve ever used VB.NET you may well have found and enjoyed the with keyword to write code such as:",{"type":17,"children":32,"toc":577},[33,37,41,210,215,293,298,303,308,387,392,557,562,571],{"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-vb shiki shiki-themes everforest-light dracula","Dim myObj As MyClass\nmyObj = New MyClass()\nWith myObj\n  .ProductCode = \"ABC123\"\n  .Quantity = 5\n  .Cost = 567.89\nEnd With\n","vb","",[49],{"type":20,"tag":50,"props":51,"children":52},"code",{"__ignoreMap":47},[53,83,113,128,158,177,195],{"type":20,"tag":54,"props":55,"children":58},"span",{"class":56,"line":57},"line",1,[59,65,71,77],{"type":20,"tag":54,"props":60,"children":62},{"style":61},"--shiki-default:#3A94C5;--shiki-dark:#FF79C6",[63],{"type":25,"value":64},"Dim",{"type":20,"tag":54,"props":66,"children":68},{"style":67},"--shiki-default:#5C6A72;--shiki-dark:#F8F8F2",[69],{"type":25,"value":70}," myObj ",{"type":20,"tag":54,"props":72,"children":74},{"style":73},"--shiki-default:#F57D26;--shiki-dark:#FF79C6",[75],{"type":25,"value":76},"As",{"type":20,"tag":54,"props":78,"children":80},{"style":79},"--shiki-default:#3A94C5;--shiki-default-font-style:inherit;--shiki-dark:#8BE9FD;--shiki-dark-font-style:italic",[81],{"type":25,"value":82}," MyClass\n",{"type":20,"tag":54,"props":84,"children":86},{"class":56,"line":85},2,[87,92,97,102,108],{"type":20,"tag":54,"props":88,"children":89},{"style":67},[90],{"type":25,"value":91},"myObj ",{"type":20,"tag":54,"props":93,"children":94},{"style":73},[95],{"type":25,"value":96},"=",{"type":20,"tag":54,"props":98,"children":99},{"style":61},[100],{"type":25,"value":101}," New ",{"type":20,"tag":54,"props":103,"children":105},{"style":104},"--shiki-default:#8DA101;--shiki-dark:#50FA7B",[106],{"type":25,"value":107},"MyClass",{"type":20,"tag":54,"props":109,"children":110},{"style":67},[111],{"type":25,"value":112},"()\n",{"type":20,"tag":54,"props":114,"children":116},{"class":56,"line":115},3,[117,123],{"type":20,"tag":54,"props":118,"children":120},{"style":119},"--shiki-default:#F85552;--shiki-dark:#FF79C6",[121],{"type":25,"value":122},"With",{"type":20,"tag":54,"props":124,"children":125},{"style":67},[126],{"type":25,"value":127}," myObj\n",{"type":20,"tag":54,"props":129,"children":131},{"class":56,"line":130},4,[132,137,141,147,153],{"type":20,"tag":54,"props":133,"children":134},{"style":67},[135],{"type":25,"value":136},"  .ProductCode ",{"type":20,"tag":54,"props":138,"children":139},{"style":73},[140],{"type":25,"value":96},{"type":20,"tag":54,"props":142,"children":144},{"style":143},"--shiki-default:#DFA000;--shiki-dark:#E9F284",[145],{"type":25,"value":146}," \"",{"type":20,"tag":54,"props":148,"children":150},{"style":149},"--shiki-default:#DFA000;--shiki-dark:#F1FA8C",[151],{"type":25,"value":152},"ABC123",{"type":20,"tag":54,"props":154,"children":155},{"style":143},[156],{"type":25,"value":157},"\"\n",{"type":20,"tag":54,"props":159,"children":161},{"class":56,"line":160},5,[162,167,171],{"type":20,"tag":54,"props":163,"children":164},{"style":67},[165],{"type":25,"value":166},"  .Quantity ",{"type":20,"tag":54,"props":168,"children":169},{"style":73},[170],{"type":25,"value":96},{"type":20,"tag":54,"props":172,"children":174},{"style":173},"--shiki-default:#DF69BA;--shiki-dark:#BD93F9",[175],{"type":25,"value":176}," 5\n",{"type":20,"tag":54,"props":178,"children":180},{"class":56,"line":179},6,[181,186,190],{"type":20,"tag":54,"props":182,"children":183},{"style":67},[184],{"type":25,"value":185},"  .Cost ",{"type":20,"tag":54,"props":187,"children":188},{"style":73},[189],{"type":25,"value":96},{"type":20,"tag":54,"props":191,"children":192},{"style":173},[193],{"type":25,"value":194}," 567.89\n",{"type":20,"tag":54,"props":196,"children":198},{"class":56,"line":197},7,[199,205],{"type":20,"tag":54,"props":200,"children":202},{"style":201},"--shiki-default:#8DA101;--shiki-dark:#8BE9FD",[203],{"type":25,"value":204},"End",{"type":20,"tag":54,"props":206,"children":207},{"style":119},[208],{"type":25,"value":209}," With\n",{"type":20,"tag":21,"props":211,"children":212},{},[213],{"type":25,"value":214},"This is more concise than writing myObj several times over, especially if setting a large number of properties, but as C# has no such keyword many people resorted to providing helpful constructors to facilitate code like:",{"type":20,"tag":42,"props":216,"children":220},{"className":217,"code":218,"language":219,"meta":47,"style":47},"language-csharp shiki shiki-themes everforest-light dracula","MyClass myObj = new MyClass(\"ABC123\", 5, 567.89);\n","csharp",[221],{"type":20,"tag":50,"props":222,"children":223},{"__ignoreMap":47},[224],{"type":20,"tag":54,"props":225,"children":226},{"class":56,"line":57},[227,231,235,239,244,249,254,260,265,269,274,279,283,288],{"type":20,"tag":54,"props":228,"children":229},{"style":79},[230],{"type":25,"value":107},{"type":20,"tag":54,"props":232,"children":233},{"style":67},[234],{"type":25,"value":70},{"type":20,"tag":54,"props":236,"children":237},{"style":73},[238],{"type":25,"value":96},{"type":20,"tag":54,"props":240,"children":241},{"style":119},[242],{"type":25,"value":243}," new",{"type":20,"tag":54,"props":245,"children":246},{"style":79},[247],{"type":25,"value":248}," MyClass",{"type":20,"tag":54,"props":250,"children":251},{"style":67},[252],{"type":25,"value":253},"(",{"type":20,"tag":54,"props":255,"children":257},{"style":256},"--shiki-default:#8DA101;--shiki-dark:#E9F284",[258],{"type":25,"value":259},"\"",{"type":20,"tag":54,"props":261,"children":263},{"style":262},"--shiki-default:#8DA101;--shiki-dark:#F1FA8C",[264],{"type":25,"value":152},{"type":20,"tag":54,"props":266,"children":267},{"style":256},[268],{"type":25,"value":259},{"type":20,"tag":54,"props":270,"children":271},{"style":67},[272],{"type":25,"value":273},", ",{"type":20,"tag":54,"props":275,"children":276},{"style":173},[277],{"type":25,"value":278},"5",{"type":20,"tag":54,"props":280,"children":281},{"style":67},[282],{"type":25,"value":273},{"type":20,"tag":54,"props":284,"children":285},{"style":173},[286],{"type":25,"value":287},"567.89",{"type":20,"tag":54,"props":289,"children":290},{"style":67},[291],{"type":25,"value":292},");\n",{"type":20,"tag":21,"props":294,"children":295},{},[296],{"type":25,"value":297},"If all three of these properties are essential then this makes for a sensible constructor however many classes have a number of properties that are optional and class designers struggle to determine whether to make constructors that merely cut-down on typing and which of the various combinations of optional properties might make sense in having their own constructor.",{"type":20,"tag":21,"props":299,"children":300},{},[301],{"type":25,"value":302},"Invariably the combination you might want doesn’t exist and if it does the chances of being able to understand which properties are being set from one of a number of constructors that take parameters of similar types is low unless you go and take a peek with the IntelliSense.",{"type":20,"tag":21,"props":304,"children":305},{},[306],{"type":25,"value":307},"Using object initializers you can stick to creating constructors that reflect parameters necessary to ensure your object is in a valid state and forget about providing helpful ones for those optional parameters. In our example if we assume the ProductCode is essential and the others are optional we can write code like:",{"type":20,"tag":42,"props":309,"children":311},{"className":217,"code":310,"language":219,"meta":47,"style":47},"MyClass myObj = new MyClass(\"ABC123\") { Quantity = 5, Cost = 567.89 };\n",[312],{"type":20,"tag":50,"props":313,"children":314},{"__ignoreMap":47},[315],{"type":20,"tag":54,"props":316,"children":317},{"class":56,"line":57},[318,322,326,330,334,338,342,346,350,354,359,363,368,373,377,382],{"type":20,"tag":54,"props":319,"children":320},{"style":79},[321],{"type":25,"value":107},{"type":20,"tag":54,"props":323,"children":324},{"style":67},[325],{"type":25,"value":70},{"type":20,"tag":54,"props":327,"children":328},{"style":73},[329],{"type":25,"value":96},{"type":20,"tag":54,"props":331,"children":332},{"style":119},[333],{"type":25,"value":243},{"type":20,"tag":54,"props":335,"children":336},{"style":79},[337],{"type":25,"value":248},{"type":20,"tag":54,"props":339,"children":340},{"style":67},[341],{"type":25,"value":253},{"type":20,"tag":54,"props":343,"children":344},{"style":256},[345],{"type":25,"value":259},{"type":20,"tag":54,"props":347,"children":348},{"style":262},[349],{"type":25,"value":152},{"type":20,"tag":54,"props":351,"children":352},{"style":256},[353],{"type":25,"value":259},{"type":20,"tag":54,"props":355,"children":356},{"style":67},[357],{"type":25,"value":358},") { Quantity ",{"type":20,"tag":54,"props":360,"children":361},{"style":73},[362],{"type":25,"value":96},{"type":20,"tag":54,"props":364,"children":365},{"style":173},[366],{"type":25,"value":367}," 5",{"type":20,"tag":54,"props":369,"children":370},{"style":67},[371],{"type":25,"value":372},", Cost ",{"type":20,"tag":54,"props":374,"children":375},{"style":73},[376],{"type":25,"value":96},{"type":20,"tag":54,"props":378,"children":379},{"style":173},[380],{"type":25,"value":381}," 567.89",{"type":20,"tag":54,"props":383,"children":384},{"style":67},[385],{"type":25,"value":386}," };\n",{"type":20,"tag":21,"props":388,"children":389},{},[390],{"type":25,"value":391},"Which is both concise and easy to understand. It also requires no work on the part of the class designer and therefore works with all your existing classes. You can also nest them to set properties that require more complex types such as:",{"type":20,"tag":42,"props":393,"children":395},{"className":217,"code":394,"language":219,"meta":47,"style":47},"MyClass myObj = new MyClass(\"ABC123\") {\n   Quantity = 5,\n   Cost = 567.89,\n   Category = new Category(\"A\") { Description = \"New machine\" }\n};\n",[396],{"type":20,"tag":50,"props":397,"children":398},{"__ignoreMap":47},[399,443,464,484,549],{"type":20,"tag":54,"props":400,"children":401},{"class":56,"line":57},[402,406,410,414,418,422,426,430,434,438],{"type":20,"tag":54,"props":403,"children":404},{"style":79},[405],{"type":25,"value":107},{"type":20,"tag":54,"props":407,"children":408},{"style":67},[409],{"type":25,"value":70},{"type":20,"tag":54,"props":411,"children":412},{"style":73},[413],{"type":25,"value":96},{"type":20,"tag":54,"props":415,"children":416},{"style":119},[417],{"type":25,"value":243},{"type":20,"tag":54,"props":419,"children":420},{"style":79},[421],{"type":25,"value":248},{"type":20,"tag":54,"props":423,"children":424},{"style":67},[425],{"type":25,"value":253},{"type":20,"tag":54,"props":427,"children":428},{"style":256},[429],{"type":25,"value":259},{"type":20,"tag":54,"props":431,"children":432},{"style":262},[433],{"type":25,"value":152},{"type":20,"tag":54,"props":435,"children":436},{"style":256},[437],{"type":25,"value":259},{"type":20,"tag":54,"props":439,"children":440},{"style":67},[441],{"type":25,"value":442},") {\n",{"type":20,"tag":54,"props":444,"children":445},{"class":56,"line":85},[446,451,455,459],{"type":20,"tag":54,"props":447,"children":448},{"style":67},[449],{"type":25,"value":450},"   Quantity ",{"type":20,"tag":54,"props":452,"children":453},{"style":73},[454],{"type":25,"value":96},{"type":20,"tag":54,"props":456,"children":457},{"style":173},[458],{"type":25,"value":367},{"type":20,"tag":54,"props":460,"children":461},{"style":67},[462],{"type":25,"value":463},",\n",{"type":20,"tag":54,"props":465,"children":466},{"class":56,"line":115},[467,472,476,480],{"type":20,"tag":54,"props":468,"children":469},{"style":67},[470],{"type":25,"value":471},"   Cost ",{"type":20,"tag":54,"props":473,"children":474},{"style":73},[475],{"type":25,"value":96},{"type":20,"tag":54,"props":477,"children":478},{"style":173},[479],{"type":25,"value":381},{"type":20,"tag":54,"props":481,"children":482},{"style":67},[483],{"type":25,"value":463},{"type":20,"tag":54,"props":485,"children":486},{"class":56,"line":130},[487,492,496,500,505,509,513,518,522,527,531,535,540,544],{"type":20,"tag":54,"props":488,"children":489},{"style":67},[490],{"type":25,"value":491},"   Category ",{"type":20,"tag":54,"props":493,"children":494},{"style":73},[495],{"type":25,"value":96},{"type":20,"tag":54,"props":497,"children":498},{"style":119},[499],{"type":25,"value":243},{"type":20,"tag":54,"props":501,"children":502},{"style":79},[503],{"type":25,"value":504}," Category",{"type":20,"tag":54,"props":506,"children":507},{"style":67},[508],{"type":25,"value":253},{"type":20,"tag":54,"props":510,"children":511},{"style":256},[512],{"type":25,"value":259},{"type":20,"tag":54,"props":514,"children":515},{"style":262},[516],{"type":25,"value":517},"A",{"type":20,"tag":54,"props":519,"children":520},{"style":256},[521],{"type":25,"value":259},{"type":20,"tag":54,"props":523,"children":524},{"style":67},[525],{"type":25,"value":526},") { Description ",{"type":20,"tag":54,"props":528,"children":529},{"style":73},[530],{"type":25,"value":96},{"type":20,"tag":54,"props":532,"children":533},{"style":256},[534],{"type":25,"value":146},{"type":20,"tag":54,"props":536,"children":537},{"style":262},[538],{"type":25,"value":539},"New machine",{"type":20,"tag":54,"props":541,"children":542},{"style":256},[543],{"type":25,"value":259},{"type":20,"tag":54,"props":545,"children":546},{"style":67},[547],{"type":25,"value":548}," }\n",{"type":20,"tag":54,"props":550,"children":551},{"class":56,"line":160},[552],{"type":20,"tag":54,"props":553,"children":554},{"style":67},[555],{"type":25,"value":556},"};\n",{"type":20,"tag":21,"props":558,"children":559},{},[560],{"type":25,"value":561},"This feature is no use if your objects are immutable in which case constructors are your only friend.",{"type":20,"tag":21,"props":563,"children":564},{},[565],{"type":20,"tag":566,"props":567,"children":568},"em",{},[569],{"type":25,"value":570},"[)amien",{"type":20,"tag":572,"props":573,"children":574},"style",{},[575],{"type":25,"value":576},"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":85,"depth":85,"links":578},[],"markdown","content:blog:2007:object-initializers-in-net-35.md","content","blog/2007/object-initializers-in-net-35.md","blog/2007/object-initializers-in-net-35","md","/blog/2007/object-initializers-in-net-35/",430,0,[589,593,597],{"title":590,"date":591,"url":592},"HTML5 Video Cheatsheet: Optimizing videos for the web","2025-12-05T00:00:00Z","/blog/2025/html5-video-cheatsheet/",{"title":594,"date":595,"url":596},"Transactions in the MongoDB EF Core Provider","2025-10-25","/blog/2025/mongodb-explicit-transactions/",{"title":598,"date":599,"url":600},"Queryable Encryption with the MongoDB EF Core Provider","2025-09-22","/blog/2025/mongodb-queryable-encryption/",[],1779264590332]