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 178729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:16:55+00:00 2026-05-11T14:16:55+00:00

What are the differences between the two and when would you use an object

  • 0

What are the differences between the two and when would you use an ‘object initializer’ over a ‘constructor’ and vice-versa? I’m working with C#, if that matters. Also, is the object initializer method specific to C# or .NET?

  • 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. 2026-05-11T14:16:56+00:00Added an answer on May 11, 2026 at 2:16 pm

    Object Initializers were something added to C# 3, in order to simplify construction of objects when you’re using an object.

    Constructors run, given 0 or more parameters, and are used to create and initialize an object before the calling method gets the handle to the created object. For example:

    MyObject myObjectInstance = new MyObject(param1, param2); 

    In this case, the constructor of MyObject will be run with the values param1 and param2. These are both used to create the new MyObject in memory. The created object (which is setup using those parameters) gets returned, and set to myObjectInstance.

    In general, it’s considered good practice to have a constructor require the parameters needed in order to completely setup an object, so that it’s impossible to create an object in an invalid state.

    However, there are often ‘extra’ properties that could be set, but are not required. This could be handled through overloaded constructors, but leads to having lots of constructors that aren’t necessarily useful in the majority of circumstances.

    This leads to object initializers – An Object Initializer lets you set properties or fields on your object after it’s been constructed, but before you can use it by anything else. For example:

    MyObject myObjectInstance = new MyObject(param1, param2) {     MyProperty = someUsefulValue }; 

    This will behave about the same as if you do this:

    MyObject myObjectInstance = new MyObject(param1, param2); myObjectInstance.MyProperty = someUsefulValue; 

    However, in multi-threaded environments the atomicity of the object initializer may be beneficial, since it prevents the object from being in a not-fully initialized state (see this answer for more details) – it’s either null or initialized like you intended.

    Also, object initializers are simpler to read (especially when you set multiple values), so they give you the same benefit as many overloads on the constructor, without the need to have many overloads complicating the API for that class.

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

Sidebar

Ask A Question

Stats

  • Questions 93k
  • Answers 94k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You have two identical base addresses defined: <service name="SUR.Core.Service.Implementation.SURDirectoryService" behaviorConfiguration="DefaultServiceBehavior">… May 11, 2026 at 6:44 pm
  • Editorial Team
    Editorial Team added an answer I don't see why an ABC couldn't properly have per-instance… May 11, 2026 at 6:44 pm
  • Editorial Team
    Editorial Team added an answer The clasical dragon book explains very well how LR parsers… May 11, 2026 at 6:44 pm

Related Questions

I haven't play enough with this and usually use mocks, but I wonder what
In a progression of languages, I have been learning C and C++. Now I
I've noticed a lot of talk about asp.net MVC lately, but I haven't come
I've been jumping from C# to Java an awful lot and the differences between

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.