By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,426 Members | 1,689 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,426 IT Pros & Developers. It's quick & easy.

Passing large amounts of data between classes/functions

P: n/a
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
Share this Question
Share on Google+
11 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.