c#, when to use a structure instead of a class?
Page 1 of 1
CraweN




Posts: 719
Location: Upside down in chair
PostPosted: Sun, 7th Mar 2010 01:39    Post subject: c#, when to use a structure instead of a class?
Hey peeps.

is there any easy way to calculate when to use a structure instead of a class. I'm still a beginner on programming, but is there some rule of thumb on when to use a struct as to using a class?

regards
CraweN


Hardware: Ryzen 3700x, B450 MSI Gaming Pro carbon AC, GTX1080, 32 GB 3200 Mhz cas 14, 256 EVO SSD, 1 TB EVO SSD and 4 TB HDD.
Console: xbox, wii and xbox 360
Back to top
panzieman




Posts: 288

PostPosted: Sun, 7th Mar 2010 07:10    Post subject:
As a beginner in programming just always use a class. You really only want to use structs when you're really, really trying to keep your memory footprint down. But at that point you should be using a language that more easily lends itself to efficient programming than C#.
Back to top
tainted4ever
VIP Member



Posts: 11335

PostPosted: Sun, 7th Mar 2010 07:47    Post subject:
If you're only getting and setting stuff use a structure. Soon as you want to manipulate the data, use a class. There's really no formula to use. Just do what you think is right. If you're a beginner, stick to using classes.


Sense Amid Madness, Wit Amidst Folly
Back to top
LeoNatan
Banned



Posts: 73193
Location: Ramat Gan, Israel 🇮🇱
PostPosted: Tue, 9th Mar 2010 15:28    Post subject:
One thing to remember is that structures are sent by value, meaning that every time you sent it to a function, it is copied locally. This process takes time and space, so when thinking of using structures, take that in mind. You can however offset this by changing methods to receive these structures by reference by using the "ref" keyword. Structures are best for storing a small information together, which can usually be seen as a data type comparable to ints, floats, etc. For example, a vector would be a nice example of a structure. And yet, I just made all my methods in an XNA (3D) project take structures by references, and I saw a considerable performance boost, which would increase on machines with lower specs. Smile
Back to top
Page 1 of 1 All times are GMT + 1 Hour
NFOHump.com Forum Index - Programmers Corner
Signature/Avatar nuking: none (can be changed in your profile)  


Display posts from previous:   

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB 2.0.8 © 2001, 2002 phpBB Group