Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 854647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:57:24+00:00 2026-05-15T07:57:24+00:00

I was too excited when object initializer appeared in C#. MyClass a = new

  • 0

I was too excited when object initializer appeared in C#.

MyClass a = new MyClass();
a.Field1 = Value1;
a.Field2 = Value2;

can be rewritten shorter:

MyClass a = new MyClass { Field1 = Value1, Field2 = Value2 }

Object initializer code is more obvious but when properties number come to dozen and some of the assignment deals with nullable values it’s hard to debug where the “null reference error” is. Studio shows the whole object initializer as error point.

Nowadays I use object initializer for straightforward assignment only for error-free properties.

How do you use object initializer for complex assignment or it’s a bad practice to use dozen of assigments at all?

Thank you in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T07:57:25+00:00Added an answer on May 15, 2026 at 7:57 am

    Well, my main beef with object initializers is that they require a mutable type to start with: where possible, I prefer to use immutable types. Having said that, when an object initializer will work (e.g. for UI controls) I tend to use them.

    I would possibly think twice if the values for the assignments were particularly complicated to compute – in particular, you have to be able to get the value in a single expression, and that may end up being less readable than computing it over several statements… but that’s relatively rare in situations where this is feasible to start with.

    I can’t say I’ve had any problems with exceptions during property assignments with object initializers – it’s just not something that comes up for me. If it did, I’d probably try to write a failing unit test anyway, at which point code usually becomes easy to fix without the debugger anyway.

    Obviously moderation is always a good thing – I’m not suggesting taking this to extremes… but if you’re setting a dozen properties, using an object initializer wouldn’t be as much of a concern to me as having a dozen properties to set to start with. Are any of these dozen properties related? Should those be encapsulated together somehow? (In some cases that is fine – again, particularly with UI controls – but often it’s not.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Its too much code to paste here so, I have created demo please have
my code is too long so sorry :) @model IEnumerable<Osos11.Models.Customers> @Html.DevExpress().GridView( settings => {
Not too familiar with dtsx packages - can they be created/run outisde Visual Studio?
After too many hours I can't find the answer to this. First my table
Not too familiar with GSA here so hopefully someone can help me. Have a
I too have a long running service using plugins and appdomains and am having
Not too sure what's going on here as this doesn't seem like standard practise
Not too sure how to formulate my question and I hope that this is
Not too sure if I'm doing this right. It looks a bit more convoluted
I am not too clear about the autorelease message... When autorelease is sent to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.