473,395 Members | 1,622 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Properties Retuning Arrays

Why, when a property returns an array, why doesn't it return a reference to
that array?

e.g.:

public string[] Array
{
get
{
return arr;
}
}

According to MSDN, a copy of the array is returned here. Why is this?

http://msdn.microsoft.com/library/de...guidelines.asp
Nov 16 '05 #1
4 1229
Cool Guy <co*****@abc.xyz> wrote:
Why, when a property returns an array, why doesn't it return a reference to
that array?
It does.
e.g.:

public string[] Array
{
get
{
return arr;
}
}

According to MSDN, a copy of the array is returned here. Why is this?


No, that's suggesting that it's a good idea to return a copy of the
array - otherwise the caller can effectively change internal state by
changing the contents of the array.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
Cool Guy <co*****@abc.xyz> wrote:
Why, when a property returns an array, why doesn't it return a reference to
that array?
It does.
e.g.:

public string[] Array
{
get
{
return arr;
}
}

According to MSDN, a copy of the array is returned here. Why is this?


No, that's suggesting that it's a good idea to return a copy of the
array - otherwise the caller can effectively change internal state by
changing the contents of the array.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
Jon Skeet [C# MVP] wrote:
No, that's suggesting that it's a good idea to return a copy of the
array - otherwise the caller can effectively change internal state by
changing the contents of the array.


Oh, of course. I just ran FxCop for the first time and it ended up
confusing me.

Thanks Jon.
Nov 16 '05 #4
Jon Skeet [C# MVP] wrote:
No, that's suggesting that it's a good idea to return a copy of the
array - otherwise the caller can effectively change internal state by
changing the contents of the array.


Oh, of course. I just ran FxCop for the first time and it ended up
confusing me.

Thanks Jon.
Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: ytrewq | last post by:
Should dynamic ("expando") properties be restricted to native and user-defined objects? Or should host objects - such as references to the browser or a plug-in or to the document and its elements -...
1
by: Jamus Sprinson | last post by:
Before I continue, I'm going to begin by saying I'm not by any means an expert- I've been using .NET with C# for about 4 months now, and basically just learning by example and docs. A game...
15
by: Gary Morris | last post by:
Hello all, OK, first of all I have known about properties since VB6, which I have and have used extensively. It seems that property get and set are basically the same concept in C# and VB.NET,...
1
by: Ben | last post by:
Hi There I am learning about reflection so apologies if this post is vague ... I am doing some unit testing and have written a function that accepts two objects and tests to see if their...
4
by: MichaelGeier | last post by:
Hello, I have a class with three overloaded indexed poperties. Declaration in *.h __gc class ByteArrayN : public FArrayN { typedef BYTE T;
7
by: steve bull | last post by:
If I create an array, e.g. private int x, inside a parent class whose elements can be accessed by a child class - what is the recommended way of doing this? When I create a property for the array...
3
by: Dan | last post by:
Is it possible to dynamically create and populate properties? Below is my class. I would like to define properties for each of the items in my two arrays without actually defining each one. (There...
1
by: dasayu | last post by:
Hi, I have a custom object called gridWidget. I am consistantly getting an error in FireFox when I click on an href, which calls a function defined on the object. The generated link looks similar...
3
by: Michael Matteson | last post by:
I have two classes. Class A and Class B. I give class A 5 properties int prop1(){} int prop2(){} int prop3(){} int prop4(){} classB prop5(){} what i would like to do is to create a 5th...
13
by: Just_a_fan | last post by:
I am adding a bunch of controls with the code below. Problem 1: When program flow passes to "UpperChanged" when I click it, the control name is undefined. When I enter: If udUpperLim1.Value 1...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.