F
Faustino Dina
Hi,
The following code is from an article published in Informit.com at
http://www.informit.com/guides/content.asp?g=dotnet&seqNum=142. The problem
is the author says it is not a good idea to return an array as a property
because it will return a copy of the array instead a reference to it. How
can I force the property to return a reference to the array? Is it only a
feature of arrays? I hope normal class objects (including collections) are
returned by reference in the same situation.
Well, I'll be glad to hear about workarounds to this array problem.
Thanks in advance
Faustino
The code follows:
class GameThing
{
private readonly int[] scoreValues = new int[22];
public GameThing()
{
for (int i = 0; i < scoreValues.Length; i++)
{
scoreValues = scoreValues.Length - i - 1;
}
}
public int[] Values
{
get { return scoreValues; }
}
}
The following code is from an article published in Informit.com at
http://www.informit.com/guides/content.asp?g=dotnet&seqNum=142. The problem
is the author says it is not a good idea to return an array as a property
because it will return a copy of the array instead a reference to it. How
can I force the property to return a reference to the array? Is it only a
feature of arrays? I hope normal class objects (including collections) are
returned by reference in the same situation.
Well, I'll be glad to hear about workarounds to this array problem.
Thanks in advance
Faustino
The code follows:
class GameThing
{
private readonly int[] scoreValues = new int[22];
public GameThing()
{
for (int i = 0; i < scoreValues.Length; i++)
{
scoreValues = scoreValues.Length - i - 1;
}
}
public int[] Values
{
get { return scoreValues; }
}
}