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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:18:05+00:00 2026-05-24T12:18:05+00:00

I was reading some of the documentation on MSDN concerning do’s and don’t with

  • 0

I was reading some of the documentation on MSDN concerning do’s and don’t with regards to whether something should be implemented as a property or as a method. I ran into one rule in particular that I have a question about.

If “The operation returns an array” use a method (instead of a property).

The page is here: Choosing Between Properties and Methods

Use a method where the operation returns an
array because to preserve the internal array, you would have to return
a deep copy of the array, not a reference to the array used by the
property. This fact, combined with the fact that developers use
properties as though they were fields, can lead to very inefficient
code.

I understand that the get method of the property would return a reference to the array, which would allow the array to be changed even if there is no set. In the example they give, they are making a deep copy of the array every time the property is accessed, I guess to avoid the possibility of this happening, and this in turn is very inefficient.

It would not be inefficient if the property just returned the reference, and didn’t do all the copying, right? And also using a method instead of a property is not going to automatically protect the list from being modified. It is pretty much the same scenario, you would still need a deep copy.

Is using a property and just returning the reference to the array always bad practice? What if you want the caller to be able to modify the array, or you do not care if they modify it? Is it still bad and why, and if so what would be the proper way to allow the caller to modify?

  • 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-24T12:18:06+00:00Added an answer on May 24, 2026 at 12:18 pm

    Can you allow the caller to modify an internal array through a property? Yes, of course, but you will take on a slew of possible issues. How you handle those issues and what you can live with is up to you.

    The MSDN advice is correct in a very strict sense. That said I have seen List<T> and T[] properties returned before from classes. If your class is a very simple POCO, this is not a big issue because then those classes are just raw data and there’s no real business logic to affect.

    That said, if I’m returning a list, and I don’t want anyone to mess with the internal list, I either return a deep copy every time, or a ReadOnlyCollection, or an iterator. For example, there’s lots of places I cache web service request calls, and when i return a cache item, I do NOT want the caller modifying that data or they’ll modify what I’m caching. Thus there I make deep copies (which is still faster than the overhead of the web service call).

    You just have to know whether your usage requires the safety or not. Is the class only for internal consumption? Or is it designed to be consumed by a wider audience and you have no idea what they are going to do with it? Those type of questions may drive your response.

    Sorry for a “it depends” answer, but it truly does depend on what your goal is and if the internals of the class are sensitive to change.

    UPDATE You can also return an iterator, I’d avoid returning IEnumerable as a superclass up-cast because it can be cast back down, but if you return an iterator instead (like using Skip(0)) you are safe (aside from still being able to modify the contained objects of course).

    For example:

    public IEnumerable<T> SomeList 
    {
        get { return _internalList.Skip(0); }
    }
    

    Is better than:

    public IEnumerable<T> SomeList
    {
        get { return _internalList; }
    }
    

    Because the later can still be cast back to List<T> or whatever it was, while the first is an iterator and can’t be modified.

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

Sidebar

Related Questions

i've been reading through the linq to xml documentation in msdn and some other
I was reading some module documentation and saw something I didn't understand, in the
A question about updatable db views: I'm reading through some MSDN documentation on the
When reading some documentation about assertions, I found: java -ea -dsa Enables assertions in
I'm trying to understand how ID3 tags work, so, after reading some documentation, I
After reading all sorts of Stackoverflow postings and various documentation including some on http://code.google.com/p/google-api-java-client/
Reading the Eigen library documentation, I noticed that some objects cannot be passed by
After searching online and reading the Oracle documentation for this, I need some help
I'm looking for some advice on how to go about reading the online documentation
Hello I'm trying something .. I was reading Jquery traversing documentations some forums, this

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.