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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T07:37:07+00:00 2026-06-07T07:37:07+00:00

I came about something rather baffling in C# just recently. In our code base,

  • 0

I came about something rather baffling in C# just recently. In our code base, we have a TreeNode class. When changing some code, I found that it was impossible to assign a variable to the Nodes property. On closer inspection it became clear that the property is read-only and this behavior is to be expected.

What is strange is that our code base had until then always relied on assignment of some anonymous type to the Nodes property and compiled and worked perfectly.

To summarize: why did the assignment in AddSomeNodes work in the first place?

using System.Collections.Generic;

namespace ReadOnlyProperty
{
    public class TreeNode
    {
        private readonly IList<TreeNode> _nodes = new List<TreeNode>();

        public IList<TreeNode> Nodes
        {
            get { return _nodes;  }
        }
    }

    public class TreeBuilder
    {
        public IEnumerable<TreeNode> AddSomeNodes()
        {
            yield return new TreeNode
             {
                Nodes = { new TreeNode() }
             };
        }

        public IEnumerable<TreeNode> AddSomeOtherNodes()
        {
            var someNodes = new List<TreeNode>();

            yield return new TreeNode
             {
                Nodes = someNodes
             };
        }
    }
}
  • 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-06-07T07:37:09+00:00Added an answer on June 7, 2026 at 7:37 am

    AddSomeNodes is not creating an instance of List<TreeNode> because that syntax is a collection initializer (therefore it is not assigning to Nodes meaning it doesn’t break the readonly contract), the compiler actually translates the collection initializer into calls to .Add.

    The AddSomeOtherNodes call actually tries to re-assign the value, but it is readonly. This is also the object initializer syntax, which translates into simple property calls. This property does not have a setter, so that call generates a compiler error. Attempting to add a setter that sets the readonly value will generate another compiler error because it is marked readonly.

    From MSDN:

    By using a collection initializer you do not have to specify multiple
    calls to the Add method of the class in your source code; the compiler
    adds the calls.

    Also, just to clarify, there are no anonymous types in play in your code – it is all initializer syntax.


    Unrelated to your question, but in the same area.

    Interestingly, the Nodes = { new TreeNode() } syntax doesn’t work with a local member, it only seems to work when it is nested inside an object initializer or during object assignment:

    List<int> numbers = { 1, 2, 3, 4 }; // This isn't valid.
    List<int> numbers = new List<int> { 1, 2, 3, 4 }; // Valid.
    
    // This is valid, but will NullReferenceException on Numbers
    // if NumberContainer doesn't "new" the list internally.
    var container = new NumberContainer()  
    {
        Numbers = { 1, 2, 3, 4 }
    };
    

    The MSDN documentation doesn’t seem to have any clarification on this.

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

Sidebar

Related Questions

I would like to clarify something about TimerTask. When you have the code below:
I came about a rather weird problem today. I have a math library optimized
I came across couple of questions about OCL expressions. After reading some university slides
I recently came across a question about sequence points in C++ at this site,
This question came about as a result of some mixed-language programming. I had a
I've been going through some old code, where I came across some custom defined
This came up as a question I asked in an interview recently as something
Just experimenting with different inheritance techniques in JS, and came across something mildly discomfiting
This is not a question about real code, it is just for discussion. It
I tried to find something about this on Google but nothing came out. I

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.