473,395 Members | 1,652 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.

Passing large amounts of data between classes/functions

Hi,

I'm writing an application that will pass a large amount of data between
classes/functions.

In C++ it was more efficient to send a pointer to the object, e.g structure
rather than passing the actual structure itself.

Is this true of C# also?

What is the best way to pass a large amount of data with different data types?

e.g object could contain some strings, doubles, bytes etc.

Regards
Macca

Jan 23 '06 #1
11 4096
Macca,

I would return a reference to a DataSet, which has the appropriate
information (if it is something where the structure is fluid), or create an
instance of a class which has the specific information, which you can then
return a reference to (instances of classes, not structures are reference
types, and you pass around the reference).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Macca" <Ma***@discussions.microsoft.com> wrote in message
news:54**********************************@microsof t.com...
Hi,

I'm writing an application that will pass a large amount of data between
classes/functions.

In C++ it was more efficient to send a pointer to the object, e.g
structure
rather than passing the actual structure itself.

Is this true of C# also?

What is the best way to pass a large amount of data with different data
types?

e.g object could contain some strings, doubles, bytes etc.

Regards
Macca

Jan 23 '06 #2
Macca wrote:
I'm writing an application that will pass a large amount of data between
classes/functions.

In C++ it was more efficient to send a pointer to the object, e.g structure
rather than passing the actual structure itself.

Is this true of C# also?

What is the best way to pass a large amount of data with different data types?

e.g object could contain some strings, doubles, bytes etc.


Unless you're dealing with large value types (structs) you don't need
to worry about this. See
http://www.pobox.com/~skeet/csharp/parameters.html

Jon

Jan 23 '06 #3
It might be worth pointing out instances are sent by reference in C#
e.g.

private void foo(MyObject obj)
{

}

'obj' is a reference. The exception to this rule is value types
(structs) which includes the String class.

Jan 23 '06 #4
Just to be clear... strings are reference types ... it sounds like your
calling them value types.
"john doe" <sl********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
It might be worth pointing out instances are sent by reference in C#
e.g.

private void foo(MyObject obj)
{

}

'obj' is a reference. The exception to this rule is value types
(structs) which includes the String class.

Jan 23 '06 #5
Hi John,

Intersting article. So if I want to pass around a data structure that
contains different types it is best to use a reference object such as a
class? rather than a value type such as a structure?

I am thinking from a performance view.

My app will have 6-7 modules (classes) and will be passing the above data
structure between them.

Regards
Macca

"Jon Skeet [C# MVP]" wrote:
Macca wrote:
I'm writing an application that will pass a large amount of data between
classes/functions.

In C++ it was more efficient to send a pointer to the object, e.g structure
rather than passing the actual structure itself.

Is this true of C# also?

What is the best way to pass a large amount of data with different data types?

e.g object could contain some strings, doubles, bytes etc.


Unless you're dealing with large value types (structs) you don't need
to worry about this. See
http://www.pobox.com/~skeet/csharp/parameters.html

Jon

Jan 23 '06 #6
Hi Nicholas,

Thanks for the advice.

Just to let you know my application will consist of 6-7 modules(classes) and
this data structure(consisting of various data types) will be used to pass
data between them. Is it best to use a class for the data structure rather
than any other data type?

Regards
Macca

"Nicholas Paldino [.NET/C# MVP]" wrote:
Macca,

I would return a reference to a DataSet, which has the appropriate
information (if it is something where the structure is fluid), or create an
instance of a class which has the specific information, which you can then
return a reference to (instances of classes, not structures are reference
types, and you pass around the reference).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Macca" <Ma***@discussions.microsoft.com> wrote in message
news:54**********************************@microsof t.com...
Hi,

I'm writing an application that will pass a large amount of data between
classes/functions.

In C++ it was more efficient to send a pointer to the object, e.g
structure
rather than passing the actual structure itself.

Is this true of C# also?

What is the best way to pass a large amount of data with different data
types?

e.g object could contain some strings, doubles, bytes etc.

Regards
Macca


Jan 23 '06 #7
Macca,

If the data structure is large, and you have to pass it around often,
then yes, a class is better, since you will be copying the data on each pass
of the data from one method to the next. If the structure is large, you
will see some performance impact.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Macca" <Ma***@discussions.microsoft.com> wrote in message
news:ED**********************************@microsof t.com...
Hi Nicholas,

Thanks for the advice.

Just to let you know my application will consist of 6-7 modules(classes)
and
this data structure(consisting of various data types) will be used to pass
data between them. Is it best to use a class for the data structure rather
than any other data type?

Regards
Macca

"Nicholas Paldino [.NET/C# MVP]" wrote:
Macca,

I would return a reference to a DataSet, which has the appropriate
information (if it is something where the structure is fluid), or create
an
instance of a class which has the specific information, which you can
then
return a reference to (instances of classes, not structures are reference
types, and you pass around the reference).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Macca" <Ma***@discussions.microsoft.com> wrote in message
news:54**********************************@microsof t.com...
> Hi,
>
> I'm writing an application that will pass a large amount of data
> between
> classes/functions.
>
> In C++ it was more efficient to send a pointer to the object, e.g
> structure
> rather than passing the actual structure itself.
>
> Is this true of C# also?
>
> What is the best way to pass a large amount of data with different data
> types?
>
> e.g object could contain some strings, doubles, bytes etc.
>
> Regards
> Macca
>


Jan 23 '06 #8
Macca wrote:
Intersting article. So if I want to pass around a data structure that
contains different types it is best to use a reference object such as a
class? rather than a value type such as a structure?

I am thinking from a performance view.

My app will have 6-7 modules (classes) and will be passing the above data
structure between them.


I very, very rarely write my own value types. Performance is usually
less important than getting the right semantics, to be honest - and
reference semantics *tend* to be preferable to value semantics IMO. Of
course, just because they're reference types doesn't mean they have to
be mutable... often an immutable reference type (such as string) gives
the best performance *and* semantics.

Jon

Jan 23 '06 #9
In reference to the above about strings, they behave like value types.
I don't know the internal workings well enough to say whether a
reference is passed or not, however use them like value types is the
easiest solution. e.g.:

private void button1_Click(object sender, System.EventArgs e)
{
string s = "moo";
this.foo(s);
MessageBox.Show(s);
}

private void foo(string s)
{
s = "hello";
}

Displays a message box with "Moo", not "hello".

Jan 24 '06 #10
This is the normal behaviour for both reference and value types (when
re-assigning the argument without the ref keyword); I think you are
confusing reference types with the ref keyword - but they mean different
things.

Marc
Jan 24 '06 #11
Chris S. wrote:
In reference to the above about strings, they behave like value types.
Well, I think it's more accurate to say that they behave like immutable
reference types. When you pass them around, a reference is passed, not
the actual text data.

If you treat it like a value type, you *might* be tempted to pass it by
reference for efficiency reasons if you've got a large string - when in
fact it would have no performance benefit (and may even harm
performance slightly).
I don't know the internal workings well enough to say whether a
reference is passed or not, however use them like value types is the
easiest solution. e.g.:

private void button1_Click(object sender, System.EventArgs e)
{
string s = "moo";
this.foo(s);
MessageBox.Show(s);
}

private void foo(string s)
{
s = "hello";
}

Displays a message box with "Moo", not "hello".


Indeed, and if you use any other type it would do the same thing there,
because s is not passed *by* reference. Changing the value of a
parameter doesn't change the value used for the argument unless it's an
argument which is passed by reference.

Now, not that changing the contents of an object that a parameter
refers to is not the same thing.

To make this concrete, I think we can all agree that StringBuilder is a
reference type. If you changed your foo method to:

private void foo (StringBuilder sb)
{
sb = new StringBuilder ("hello");
}

you'd still see the same behaviour - the caller wouldn't notice
anything.

If you changed the method to:

private void foo (StringBuilder sb)
{
sb.Append ("hello");
}

*then* they'd notice a difference.

And that highlights the difference between String and StringBuilder -
String doesn't have any methods which change the data stored in the
String; it's immutable.

Knowing this difference is important because it suggests that often
when you want what are often called "value type semantics" you can
achieve them with something which is still very definitely a reference
type, by making it immutable (at least to the outside world) in the
same way that String is immutable.

Jon

Jan 24 '06 #12

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

Similar topics

2
by: flamesrock | last post by:
Hi, Basically, what I'm trying to do is store large amounts of data in a list or dictionary and then convert that to a custom formatted xml file. My list looks roughly like this: (d,r,p]]])...
1
by: michaaal | last post by:
If I use a form to pass data (say, for example, through a textbox) the data seems to be limited to somewhat smaller amounts. What should I do if I want to pass a large amount of data? For example...
10
by: Digety | last post by:
We are looking to store a large amount of user data that will be changed and accessed daily by a large number of people. We expect around 6-8 million subscribers to our service with each record...
4
by: oshanahan | last post by:
Does anyone have ideas on the best way to move large amounts of data between tables? I am doing several simple insert/select statements from a staging table to several holding tables, but because...
3
by: Wayne Marsh | last post by:
Hi all. I am working on an audio application which needs reasonably fast access to large amounts of data. For example, the program may load a 120 second stereo sound sample stored at 4bytes per...
2
by: Dennis C. Drumm | last post by:
What is the best way to add several pages of text to a readonly TextBox? The text does not change and was created in a Word rtf document but could as easly be put in a ASCII text file. Can this be...
7
by: =?Utf-8?B?TW9iaWxlTWFu?= | last post by:
Hello everyone: I am looking for everyone's thoughts on moving large amounts (actually, not very large, but large enough that I'm throwing exceptions using the default configurations). We're...
4
by: bcomeara | last post by:
I am writing a program which needs to include a large amount of data. Basically, the data are p values for different possible outcomes from trials with different number of observations (the p...
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
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: 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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.