- Home /
What's the difference between a struct and a class?
I noticed that some entries in the API under "Runtime Classes" don't have "Class" as description, but "Struct".
So what's the difference?
Thanks & Greetz, Ky.
Answer by Mortim · Dec 26, 2009 at 12:12 AM
There are many differences between structs and classes. Basically, structs are meant for lightweights objects or data grouping, and do not allow inheritance.
Here is a detailed comparison between structs and classes in C#
http://www.jaggersoft.com/pubs/StructsVsClasses.htm
You can also check the official msdn documentation on structs data types, which I cannot post here due to the one link limitation for new users. Google "msdn struct" and you should find it.
Oh, wow Oo ... I didn't think there were THIS many differences as they are both listed among "Runtime Classes" after all...
Thank you for your answer, I'm sure this is helpful to a lot of people. =)
For me however, being a bloody newbe to program$$anonymous$$g, I don't understand as much as half of what your link tries to tell me ^^' Would probably take me several full days to work through it -.- I'd really apreciate some simplified explaination... pretty please? :) (as well as a few words as to wether JavaScript handles structs exactly the same in this case...)
You can view a struct as a simplified class. From a user perspective, it should not make many differences wether your object comes from a struct or a class. From a design perspective, struct are commonly used for simple and lightweight objects.
As far as I know, there is no struct in javascript. However you can emulate them by using simple objects. See here for an example : http://bytes.com/topic/javascript/answers/89608-datastructures-javascript
A $$anonymous$$or correction (this is also in the link you posted): One major difference between structs and classes is that the instances of structs live in the stack, while classes live in the heap. That's why very frequently used structs like Vector3 are implemented as structs ins$$anonymous$$d of classes. In C#, there's this one tricky thing that you can't change member variables of structs when those structs are properties of other objects (try transform.position.x = 3F; in C# to see what I mean).
Your answer
Follow this Question
Related Questions
Not every Class in the API is derived from the "Object"-Class, so what are they? 2 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Does Unity support multiple Inheritance? 3 Answers
Does Unity support the declaration of several classes in one script? 2 Answers
What is a Script when it's attatched to a GameObject? An Object? A Class? A Reference? 3 Answers