Struct vs Classes c#

So I’ve been thinking, when do you choose to use a struct or a class? From what I interpret based on my programming experience, I’d just use a class everytime. I know that structs are supposed to be more ‘lightweight’ or whatever, but I wouldn’t choose to use a struct over a class just because of this. From what I understand, classes can do everything a struct can do and alot more. I know that you’d typically use a struct over a class if the information it is holding is very small, but if I want to add on to the struct and add more information or even a few methods/functions then I’d have to rewrite my struct as a class because of the limitations of a struct.

Also, I know that structs are also used to organize code better, saving all your classes for the more important stuff. But in that case, why wouldnt you use anonymous classes?

Semantically, structs should be used to represent data types, classes should be used to represent objects. There are other differences, such as how memory for structs is allocated, and how they store data. It’s not a mater of what’s more important, but more about what makes more sense in terms of how you describe your programs.

I do use structs when I code, when it’s appropriate, usually for small data structures, and when it’s something I don’t want to be expanded on via inheritance. For example, I might want to store an array of Vector3s that also have an associated boolean value. I’d use a struct for that. I wouldn’t use anonymous types because they’re used for different use cases, as far as I’m aware.

https://msdn.microsoft.com/en-us/library/aa664465(v=vs.71).aspx

3 Likes

Thanks, I think I understand it better now :slight_smile:

I like to think of structs in a equals, and always equals sort of way.

I might have a number 5, and you have a number 5. Both number’s are exactly the same. (struct)
I might have a red apple, and you a red apple. Both apples are NOT exactly the same. (class)

An object from a class may have the same values (state) as another object of the same class, but they are not the same thing. I might meet another Dylan (my name), but I am still a different Dylan.

So use a struct when you’re describing something where equal value means equals.

NOTE, because structs are always copied (unless explicitly passed by reference), large structs can be expensive on the stack. Avoid creating structs that are multiple fields members in size if you’re going to be passing them around a lot.

13 Likes

Adding methods doesn’t change the size of a struct. Often you use structs when you want something to behave “like a number”…you wouldn’t want, say, int to be a class, nor something like Vector3.

–Eric

1 Like

This has got to be the best analogy I have ever read on structs vs classes.

3 Likes

As pointed out, the big decider is if you want pass by value or pass by reference.

Performance wise there are some differences too. Structs (in the right context) go on the stack, and thus don’t ever need to be garbage collected. On the other hand copying a large struct is expensive, meaning classes are better for bigger data collections.

MSDN recommends the maximum size for a struct be 16 bytes. I’ve seen some bench marks on modern architecture that push that limit up to 64 bytes. But either way keep them small, and bench mark the code if it’s hot.

Static vs Heap allocation is a huge performance buster when using Classes or Structs. Most people in Java or C# don’t really think about this as memory is taken care of for you. In C++, you have to manage everything and you become and expert at this. Here is a good reading on memory by John Skeet.

3 Likes

When it’s about Functions etc it’s better to use classes. If you have only simple structure like xyz Item = string name,int price,int id, it’s better to use struct.

1 Like

When it comes to large data, would you not use a ScriptableObject anyway?

depends on if it is just somethign you made for runtime only data to make it easier to pass around, or if it is data you want to define before runtime. The advantage of a ScriptableObject over a normal class is that it is serialized and stored as a unity asset.

None of that is true at all. Especially it’s unlikely you’d want to be using a string in a struct.

–Eric

1 Like

ScriptableObjects are a class. And their purpose is to manage data / functionality in a way that is configurable in the editor. If you don’t want or need configuration in the editor, then they are the wrong tool for the job.

If you have a hell lot of small entities (2D-map cells, for example), you would better use structs. Otherwise, the garbage collector will be very upset and slow, and it will take times more memory to keep that data than is case of structs.

3 Likes

And here is were we run into the discussion of AoS vs SoA and SIMD.

1 Like

Not sure what this means. You don’t have to save your classes, you can just make more.

Inheritance is not really relevant. True that a struct cannot be extended, but that’s not a good reason to choose it over class. If you absolutely don’t want your class to be inherited – my opinion, a bad choice – use the sealed keyword.

There’s no reason your struct shouldn’t have methods. In fact, it’s better to have methods on the struct if they operate directly on the data. No reason to throw out encapsulation just because we don’t call it a class.

Really not sure what this means. I think it’s much more complicated than we need this topic to get.

Unfortunately, this question is getting bombarded with tangents that make it hard to find a simple answer.

Follow this rule and you will be fine 99% of the time.

Recognize that structs are passed by value while classes are passed by reference.

You will need to make exceptions if you find performance problems but don’t worry about that at first.

2 Likes

Layout out tiles and manipulating them one at a time is horrible design. With SIMD you can manipulate with 4 at a time with a saving of about 50% depending on the structure. Assembly can shave off more computer cycles as well. Look into JAI. One this project is complete, it will be the best language for gaming.

I’m sorry but is that relevant to Unity programming ie struct vs classes? Can we use SIMD in C# with Unity at present?

1 Like

Yes. Yes you can. Here. Also you can use a wrapper for C to get SIMD instructions and AVX its for Desktop only but yeah you can.

And yes it is relevant to structs as its an advantage of using stacks vs heaps.

1 Like

Performance is nice, but productivity is often more important. I’m instantly wary of any language with Blow’s name on it. I’ll still check it out.