Passing large amounts of data between classes/functions

G

Guest

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
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
J

Jon Skeet [C# MVP]

Macca said:
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
 
J

john doe

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.
 
D

D. Yates

Just to be clear... strings are reference types ... it sounds like your
calling them value types.
 
G

Guest

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
 
G

Guest

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 said:
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]
- (e-mail address removed)
Macca said:
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
 
N

Nicholas Paldino [.NET/C# MVP]

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]
- (e-mail address removed)

Macca said:
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 said:
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]
- (e-mail address removed)
Macca said:
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
 
J

Jon Skeet [C# MVP]

Macca said:
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
 
C

Chris S.

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".
 
M

Marc Gravell

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
 
J

Jon Skeet [C# MVP]

Chris said:
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
 

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

Top