473,383 Members | 1,879 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,383 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 1228
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.