struct or class

J

Jure Bogataj

Hello!

Since struct and class are very similar in C#, I was wondering why using
struct would be better in some situations than using class. Any performance
issues? What about sending struct or class as parameter to function? Is it
by value or reference?

Appreciate any thoughts on this issue!


Best regards,
Jure
 
M

Marc Gravell

Since struct and class are very similar in C#
No, struct and class are *very* different in C#; much more so than in
C++.

Jon's pages give a good overview:
http://www.pobox.com/~skeet/csharp/parameters.html
I was wondering why using struct would be better in some situations
than using class
Struct is value-type; class is reference-type. I'd use "different"
more than "better", but yes - in some scenarios it can be marginally
quicker to use a struct, as there is no indirection step to the
managed heap. However, an overweight struct (as an argument) will
typically make things worse rather than better, as the struct is
(normally) cloned (simple memcopy).
What about sending struct or class as parameter to function? Is it
by value or reference?
The type can be value-type of reference-type, and *separately*, either
can be passed by-reference or by-value. All 4 options are possible.

But read Jon's article.

Marc
 
J

Jure Bogataj

Based on that structures can contain many same elements that classes do
(constructors, constants, fields, methods, properties, indexers, operators,
events, and nested types). But I do not have some deeper knowledge about
their memory considerations, layout or usage so I would appreciate any
thoughts or links for that. So to know when to use struct and when to use
class.
For example, I have made a function in C# which accepts structure as input
parameter (structure contains fields for some search filter; I could also
write all parameters inside function, but I rather created structure for
this). Then I've heard that it is better to use class (and that structures
are only usable in an unmanaged code), and didn't quite understand why.


Best regards,
Jure
 
M

Marc Gravell

If in doubt (and it sounds like you are), then you should almost
always be using a class. It is relatively unusual to write structs in
..NET - mainly to represent composite values, such as a complex-number,
or a currency/value pair - that type of thing.

You can always make the class immutable if you want value-type-like
semantics - i.e. it can't be edited after being created.

Marc
 
G

Guest

This is a common source of confusion for C++ developers, since the difference
between struct and class in C++ is miniscule (and you could easily argue that
retaining struct was a mistake in the design of C++).
--
David Anton
http://www.tangiblesoftwaresolutions.com
Convert between VB, C#, C++, and Java
Instant C#
Instant VB
Instant C++
C++ to C# Converter
C++ to VB Converter
C++ to Java Converter
 
B

Ben Voigt [C++ MVP]

Marc Gravell said:
No, struct and class are *very* different in C#; much more so than in C++.

Jon's pages give a good overview:
http://www.pobox.com/~skeet/csharp/parameters.html

Struct is value-type; class is reference-type. I'd use "different" more
than "better", but yes - in some scenarios it can be marginally quicker to
use a struct, as there is no indirection step to the managed heap.
However, an overweight struct (as an argument) will typically make things
worse rather than better, as the struct is (normally) cloned (simple
memcopy).

When using an array, a struct can be much much much better, as you save the
indirection and also you have perfect locality improving the performance of
the CPU cache. Also the garbage collection cost depends on the number of
live objects and the number of tracking handles, and by using an array of
struct you save both.
 
M

Marc Gravell

When using an array, a struct can be much much much better
Fair enough - but you'd have to be doing some heavy crunching to
really appreciate this (so maybe a math/graphical/financial
application). And of course, in such cases you'd be advised look at
the profiler first - it could still be that this is the 0.5%, and
you'd be better focussing on something that is sucking 40% CPU.

But I take your point.

Marc
 

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