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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:32:47+00:00 2026-06-07T00:32:47+00:00

In the process of adhering to code analysis errors, I’m changing my properties to

  • 0

In the process of adhering to code analysis errors, I’m changing my properties to have private setters. Then I started trying to understand why a bit more. From some research, MS says this:

A writable collection property allows a user to replace the collection with a completely different collection.

And the answer, here, states:

Adding a public setter on a List<T> object is dangerous.

But the reason why it’s dangerous is not listed. And that’s the part where I’m curious.

If we have this collection:

public List<Foo> Foos { get; set; }

Why make the setter private? Apparently we don’t want client code to replace the collection, but if a client can remove every element, and then add whatever they want, what’s the point? Is that not the same as replacing the collection entirely? How is value provided by following this code analysis rule?

  • 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-07T00:32:51+00:00Added an answer on June 7, 2026 at 12:32 am

    Not exposing the setter prevents a situation where the collection is assigned a value of null. There’s a difference between null and a collection without any values. Consider:

    for (var value in this.myCollection){ // do something

    When there are no values (i.e. someone has called Remove on every value), nothing bad happens. When this.myCollection is null, however, a NullReferenceException will be thrown.

    Code Analysis is making the assumption that your code doesn’t check that myCollection is null before operating on it.

    It’s probably also an additional safeguard for the thread-safe collection types defined in System.Collections.Concurrent. Imagine some thread trying to replace the entire collection by overwritting it. By getting rid of the public setter, the only option the thread has is to call the thread-safe Add and Remove methods.

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

Sidebar

Related Questions

Current Process: I have a tar.gz file. (Actually, I have about 2000 of them,
A process is considered to have completed correctly in Linux if its exit status
My user authorization process looks like that. During signin process function fires following code.
What tools do you use for automated code sanity checks and adhering to the
Process Explorer has an option which I have used recently called Replace Task Manager..
Loading process image file different between VS 2005 and VS 2010 We have a
A process running as a non-administrator user does not have rights to write to
My process sometimes throws exception like dllnotfound after start. i have a monitor service
Problem: Process a CSV file and test a condition on it. Current code simply
First linux service listening process is started using the following command: obexpushd –B[00:15:83:3D:0A:57]:9 –d

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.