471,336 Members | 1,290 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,336 software developers and data experts.

When to use struct and when to use class

When deciding to use a structure or a class, what is the general "rule
of thumb"? When you're creating your data model, what factors
dictate that you use a class instead of a structure?

Structures are more lightweight than classes but for example if you're
only going to create a single instance (for whatever reason), it seems
that the added "weight" of a class would be negligible. If creating
many such instances, it might be different.

Is there any GC related issues when creating structures as opposed to
classes?

I'm just looking for a good rule of thumb on when to create structures
and when to create classes.

Jul 21 '05 #1
1 6684
Hi,

Structures are value type and does not support inheritance. As a difference
between structures and classes, I feel that since the memory to be consumed
is in stack we should use structures for creating light weighted object.
Whereas, for the classes, they are reference type and since memory in heap is
much more than that in stack, we should use the classes for creating the
higher objects.GC works only with the classes and not with the structures. I
hope it helps you

regards
Nishith Pathak
http://dotnetpathak.blogspot.com


"Chris Dunaway" wrote:
When deciding to use a structure or a class, what is the general "rule
of thumb"? When you're creating your data model, what factors
dictate that you use a class instead of a structure?

Structures are more lightweight than classes but for example if you're
only going to create a single instance (for whatever reason), it seems
that the added "weight" of a class would be negligible. If creating
many such instances, it might be different.

Is there any GC related issues when creating structures as opposed to
classes?

I'm just looking for a good rule of thumb on when to create structures
and when to create classes.

Jul 21 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Steve | last post: by
4 posts views Thread by Asfand Yar Qazi | last post: by
3 posts views Thread by Jerome Cohen | last post: by
1 post views Thread by xamalek | last post: by
reply views Thread by rosydwin | last post: by

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.