473,388 Members | 1,574 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 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 6764
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Steve | last post by:
I'll be the first to admit, I'm not entirely clear on the appropriate usage of either. From what I am reading in my books, a Struct and a Class are pretty much the same, with the difference being,...
4
by: Asfand Yar Qazi | last post by:
Sorry about this, but its driving me up the wall. First some code: typedef unsigned int size_t; struct AddOp { template<class I1, class I2> static inline float call(size_t i, const I1&...
2
by: david | last post by:
Well, as a matter of fact I_HAD_MISSED a basic thing or two, anyway, although Ollie's answer makes perfectly sense when dealing with classes, it doesn't seem to me to apply as well if you have to...
0
by: Sushma | last post by:
Hi All I'm porting my application from VC6 to VC7. There is a COM exe that has successfully registered itself with system. But when I'm using its services in other Dll I get this error. ...
3
by: Jerome Cohen | last post by:
AI am trying to call a third-party web service. this service expects an XML fragment that contains the request plus other parameter. adding the web reference created the syntax below(reference.vb)....
46
by: clintonG | last post by:
Documentation tells me how but not when, why or where... <%= Clinton Gallagher http://msdn2.microsoft.com/en-us/library/saxz13w4(VS.80).aspx
3
by: DhaneshNair | last post by:
Hi all, I hav a file which is actually linkage file (used as an reference interface between c and c++ files). And this file has got two structures in it .. When i include this file directly i...
2
by: pvl_google | last post by:
Hi, I'm trying to extend an STL class with additional iterator functionality. In the simplified example below I added an extra iterator class with a dereferencing operator. This operator...
0
by: raylopez99 | last post by:
I ran afoul of this Compiler error CS1612 recently, when trying to modify a Point, which I had made have a property. It's pointless to do this (initially it will compile, but you'll run into...
1
by: xamalek | last post by:
I was thinking that when you override a struct variable when you are subclassing the struct, then the variable in the new struct would stomp on the old one ... (i.e.) the parent variable (i) and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.