473,385 Members | 2,162 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,385 software developers and data experts.

Does each return makes copy of data?

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.
Jun 27 '08 #1
6 1187
On Tue, 22 Apr 2008 11:07:54 -0700, <hu********@gmail.comwrote:
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();
}
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
Jun 27 '08 #2
On Apr 22, 2:59 pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Tue, 22 Apr 2008 11:07:54 -0700, <huohaod...@gmail.comwrote:
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();
}

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.
Jun 27 '08 #3
On Apr 22, 2:59 pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Tue, 22 Apr 2008 11:07:54 -0700, <huohaod...@gmail.comwrote:
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();
}

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.
Jun 27 '08 #4
On Tue, 22 Apr 2008 12:21:05 -0700, <hu********@gmail.comwrote:
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.
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
Jun 27 '08 #5
On Apr 22, 3:28 pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Tue, 22 Apr 2008 12:21:05 -0700, <huohaod...@gmail.comwrote:
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.

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.
Jun 27 '08 #6
On Tue, 22 Apr 2008 13:34:57 -0700, <hu********@gmail.comwrote:
Thanks a lot Pete. Basically, I was trying to find out and confirm the
data contained in the object wouldn't be copied.
And you have found out.
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();
}
When you use "out", an alias to the original parameter is used as the
method parameter. Changes within the method are reflected in the original
parameter.

When you don't use "out", a copy of the original parameter is used as the
method parameter. Changes within the method only affect that copy and are
not reflected in the original parameter.

Again, Jon's article should go a long way toward helping you understand
how it all works. I recommend that you read it.

Pete
Jun 27 '08 #7

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

Similar topics

6
by: Juho Saarikko | last post by:
The program attached to this message makes the Python interpreter segfault randomly. I have tried both Python 2.2 which came with Debian Stable, and self-compiled Python 2.3.3 (newest I could find...
12
by: Fred Pacquier | last post by:
First off, sorry for this message-in-a-bottle-like post... I haven't been able to phrase my questions well enough to get a meaningful answer from Google in my research. OTOH, it is standard...
7
by: zalzon | last post by:
Is it good practice in C++ for a member function to return data or is it better that data is stored in private member variable and printed in the member function? should i be using int...
6
by: deejaybags | last post by:
could someone please have a look at this and tell me why it segfaults. i am confused as all hell! stringman.h void load_string(char *newString); char * remove_string(); char *...
3
by: Richard Lewis Haggard | last post by:
We are having a lot of trouble with problems relating to failures relating to 'The located assembly's manifest definition with name 'xxx' does not match the assembly reference" but none of us here...
52
by: Julie | last post by:
I'm supporting an application at work. Below are some code segments that I can't understand how they work. First let me say, I would never code this standard. I'm just really creeped out that it...
18
by: active | last post by:
Console.WriteLine(String.Equals(s, s.Clone.ToString)) s is type string. This returns True. If Clone really made a new copy I'd expect False.
6
by: Lord Zoltar | last post by:
I built a small app with VS C# Express 2008, and published it as a click-once app. I tried installing it on my local machine, and when I click on its icon from the Start menu, nothing happens....
3
by: james.kirin40 | last post by:
Hi everyone, I am using Python's re module to extract some data from html. The following code never returns, and I was wondering if someone can explain to me why. Is this a problem with my...
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.