Does each return makes copy of data?

  • Thread starter Thread starter huohaodian
  • Start date Start date
H

huohaodian

Hi

I have some code something like.

public class Info
{
public static IList GetUser() { return User.GetUser();}
}

internal class User
{
IList static GetUser() { return db.GetUser(); }
}

internal class db
{
IList static GetUser() { //some code to return an arraylist }
}

Does each return makes a copy of data, I am trying to figure out if it
is need to enhance the performance.

Thanks in advance.
 
[...]
Does each return makes a copy of data,

No. ArrayList is a reference type, and as such any time it's passed,
assigned, or returned, a reference to the data is what's copied, not the
data itself.
I am trying to figure out if it
is need to enhance the performance.

For what it's worth, that's some orthogonal to the question you asked.
The way you know if you have a need to enhance the performance of your
application is for you to measure the performance, compare it to some
criteria, and if you find it lacking, to identify the areas in your
application that are most costly with respect to your performance
measurement.

Even if the answer to the question you'd asked was "yes", there's no
reason to believe without performing that specific kind of analysis that
there would be any need to work around the repeated copying of the data.
Even if the data were being copied each time, if it wasn't causing an
actual, measured performance problem there would be no need to "enhance"
the code in order to improve its performance.

Pete



Thanks a lot for the help, based on your command can I say

the internal implementation of references to the data are the almost
same (no data is actually being copied, only the reference to data) to
the below two methods?

IList GetUser()
{
IList userList new IList();
return userList;
}

and

void GetUser(IList userList)
{
userList = new IList();
|

are
 
[...]
Does each return makes a copy of data,

No. ArrayList is a reference type, and as such any time it's passed,
assigned, or returned, a reference to the data is what's copied, not the
data itself.
I am trying to figure out if it
is need to enhance the performance.

For what it's worth, that's some orthogonal to the question you asked.
The way you know if you have a need to enhance the performance of your
application is for you to measure the performance, compare it to some
criteria, and if you find it lacking, to identify the areas in your
application that are most costly with respect to your performance
measurement.

Even if the answer to the question you'd asked was "yes", there's no
reason to believe without performing that specific kind of analysis that
there would be any need to work around the repeated copying of the data.
Even if the data were being copied each time, if it wasn't causing an
actual, measured performance problem there would be no need to "enhance"
the code in order to improve its performance.

Pete


Thanks a lot for the help, based on your command can I say

the internal implementation of references to the data are the almost
same (no data is actually being copied, only the reference to data) to
the below two methods?

IList GetUser()
{
IList userList = new IList();
return userList;
}

and

void GetUser(IList userList)
{
userList = new IList();
}

Thanks
 
No. Those two examples do very different things. The first example
produces a result that can be observed by the caller. The second does
not. Neither results in the instance of IList() being copied, but other
than that they are very different.

You could make the second produce observable results by making the method
argument an "out" parameter:

void GetUser(out IList userList)
{
userList = new IList();
}

That would still not copy the instance of IList(), but would allow the
reference to the instance to be seen by the caller.

Jon Skeet has a nice article on the subject of references, values, and how
they relate to parameter passing: http://www.skeet.org.uk/csharp/parameters.html

Whether that answers your original question, I don't know.

Pete

Thanks Pete

As your comments how about I change the second method to, then first
and second would be similar?

void GetUser (out IList userList)
{
userList = new IList();
// putting users into userList
}

Thanks again.
 
No. Those two examples do very different things. The first example
produces a result that can be observed by the caller. The second does
not. Neither results in the instance of IList() being copied, but other
than that they are very different.

You could make the second produce observable results by making the method
argument an "out" parameter:

void GetUser(out IList userList)
{
userList = new IList();
}

That would still not copy the instance of IList(), but would allow the
reference to the instance to be seen by the caller.

Jon Skeet has a nice article on the subject of references, values, and how
they relate to parameter passing: http://www.skeet.org.uk/csharp/parameters.html

Whether that answers your original question, I don't know.

Pete



Thanks Pete

As your comments how about if I change the second method to below,
then first
and second methods would be similar? basically, I am trying to figure
out when returns a userList object that contains data of user records,
whether only the reference to data would be copied or the actual data
would be copied.

void GetUser (out IList userList)
{
userList = new IList();
// putting users into userList

}

Thanks again.
 
For reference types, the actual object is copied only when you explicitly
do so. Generally this would be via the Clone() method (i.e. the class
implements ICloneable), though of course a class could implement the
functionality via a method or property of its choosing.

Basically, for reference types, if you have to ask whether the data's
being copied, it isn't. You would (or at least should) know from the
usage, and there's not anything in the C# language that would copy the
data implicitly. Copying would only be done by an explicit call to some
class member that's designed to do that.

Pete



Thanks a lot Pete. Basically, I was trying to find out and confirm the
data contained in the object wouldn't be copied.

any comments of how internal pointer or referencing is handled for the
userList object between

void GetUser(out IList userList)
{
userList = new IList();
}

and
void GetUser(IList userList)
{
userList = new IList();
}

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top