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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:41:33+00:00 2026-05-25T02:41:33+00:00

Possible Duplicate: Initializer syntax Short code sample to demonstrate (VS2010 SP1, 64-bit Win7): class

  • 0

Possible Duplicate:
Initializer syntax

Short code sample to demonstrate (VS2010 SP1, 64-bit Win7):

class A
{
    public string Name { get; set; }
}

class B
{
    public A a { get; set; }
}

// OK
A a = new A { Name = "foo" };
// Using collection initialiser syntax fails as expected:
// "Can only use array initializer expressions to assign
// to array types. Try using a new expression instead."
A a = { Name = "foo" };

// OK
B b = new B { a = new A { Name = "foo" } };
// Compiles, but throws NullReferenceException when run
B b = new B { a = { Name = "foo" } };

I was surprised to see that last line compile and thought I’d found a nifty (though inconsistent) shortcut before seeing it blow up at runtime. Does that last usage have any utility?

  • 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-25T02:41:33+00:00Added an answer on May 25, 2026 at 2:41 am

    The last line is translated to:

    B tmp = new B();
    tmp.a.Name = "foo";
    B b = tmp;
    

    And yes, it very definitely has utility – when the newly created object has a read-only property returning a mutable type. The most common use of something similar is probably for collections though:

    Person person = new Person {
        Friends = {
            new Person("Dave"),
            new Person("Bob"),
        }
    }
    

    This will fetch the list of friends from Person and add two new people to it.

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

Sidebar

Related Questions

Possible Duplicate: c++ multithread I use c++ to implement a thread class. The code
Possible Duplicate: C++ static constant string (class member) static const C++ class member initialized
Possible Duplicate: Initialize class fields in constructor or at declaration? We are arguing about
Possible Duplicate: Best Practice: Initialize class fields in constructor or at declaration? I am
Possible Duplicate: Detecting an undefined object property in JavaScript From the below JavaScript sample,
Possible Duplicate Why can't I have a non-integral static const member in a class?
Possible Duplicate: Intitialzing an array in a C++ class and modifiable lvalue problem As
Possible Duplicate: Why are C# 3.0 object initializer constructor parentheses optional? Hi all I
Possible Duplicate: Where is the “proper” place to initialize class variables in AS3 I
Possible Duplicate: What does ||= mean in Ruby? class Person attr_accessor :name class <<

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.