Class or Struct, and archive to binary data

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

This is a two parte
1. What to the performance differnace between a Class and a struct. I have a handler class which has a list containing a lot of instances of a class which purely holds data, All the operations are caried out within the handler, would I be better to use a strut instead

2. I want to extact the data from the data holder class / struct, using the hander class archive the data away into a byte steam to either place in a binaryvar in a database, or to send to another application which understands the packing of the data, How do i place all the data in the byte stream (C++ memcpy)

Thanks in advance.
 
The difference is that class is reference type (nearly same as pointer), so
you have list of pointers that point to data which resides in managed heap.

If you will uses structs than you will also have pointers but, they will
point to the stack memory. If you have large number of objects it is better
to keep them in managed heap that is they must be classes.

you can use BinaryFormatter.Serialize(...) or, you can use BinaryWriter to
write in the custom way to the stream.

Moger said:
This is a two parter
1. What to the performance differnace between a Class and a struct. I
have a handler class which has a list containing a lot of instances of a
class which purely holds data, All the operations are caried out within the
handler, would I be better to use a strut instead?
2. I want to extact the data from the data holder class / struct, using
the hander class archive the data away into a byte steam to either place in
a binaryvar in a database, or to send to another application which
understands the packing of the data, How do i place all the data in the byte
stream (C++ memcpy).
 
Vadym Stetsyak said:
The difference is that class is reference type (nearly same as pointer), so
you have list of pointers that point to data which resides in managed heap.

If you will uses structs than you will also have pointers but, they will
point to the stack memory. If you have large number of objects it is better
to keep them in managed heap that is they must be classes.

No they won't. If you have a list of value types (eg an array or an
ArrayList), those will be on the heap. Structs aren't always stored on
the stack, contrary to popular myth.

See http://www.pobox.com/~skeet/csharp/memory.html
 
Vadym Stetsyak said:
The difference is that class is reference type (nearly same as pointer), so
you have list of pointers that point to data which resides in managed heap.

If you will uses structs than you will also have pointers but, they will
point to the stack memory. If you have large number of objects it is better
to keep them in managed heap that is they must be classes.

A struct is a value type, which does not have a pointer. The actual value
of a value type is copied between variables.

The description of a value type being on the stack is a bit misleading
because it suggests an absolute condition that isn't true. A value type is
copied to the stack if it is a parameter or local variable. However, if it
is a member of another type, it is stored where its enclosing type is
stored. For an enclosing reference type, it will reside in-line with the
reference type on the heap. For an enclosing value type it will reside
wherever the enclosing value type resides, which could be either the stack,
or in-line on the heap. Here's an article that describes this in more
detail:

http://www.yoda.arachsys.com/csharp/memory.html

Joe
 
Back
Top