The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

jblack1395 (36) [Avatar] Offline
#1
I was looking at the Rect class in chapter 7 and I realized that it would probably make more sense to use a struct for immutable objects rather than a class, esp if the main thing the class does is have getters and a constructor.

Just curious, not necessarily a technical problem but I think it is better coding style to use a struct here, IMO.
jon.skeet (483) [Avatar] Offline
#2
Re: Why not use structs instead of classes in C# code
It could work either way. At 16 bytes it's a reasonably hefty struct, but not unbearably so. It's certainly not cut-and-dried in favour of it being a Rect though: there are plenty of immutable classes which only have a constructor and properties. In particular, look at what the C# 3 compiler generates for anonymous types - they're immutable classes with just constructors and accessors.

Jon