Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.Drawing.Point is a value type. Why?

Tags:

c#

.net

types

I read that System.Drawing.Point is a value type. I do not understand. Why?

like image 419
Bastien Vandamme Avatar asked May 12 '26 14:05

Bastien Vandamme


1 Answers

There are rules that microsoft tries to follow about this, they explain them very well in the MSDN, see Choosing Between Classes and Structures (The book is even better as it had lot of interesting comments)

Even if Point isn't a so good sample of this :

  • Struct should logically represents a single value (In this case a position, even if it have 2 components, but Complex numbers could also be separated in 2 parts and they are prime candidates for being structs)
  • Struct should have an instance size smaller than 16 bytes. (Ok, 2x4=8)
  • Struct should not have to be boxed frequently. (Ok this one is right)
  • BUT, Struct should be immutable (Here is the part where they don't follow their own rules, but i guess that micro-optimization gained over the rules, that anyway were written later)

As i said i guess that the fact that they haven't respected the "immutable" part is both because there weren't rules when System.Drawing was written and for speed as graphic operations could be quite sensitive to this.

I don't know if they were right or not to do it, maybe they measured some common algorithms and found that they lost too much performance in allocating temporary object and copying them over. Anyway such optimizations should only be done after carefully measuring real-world usage of the class/struc.

like image 68
Julien Roncaglia Avatar answered May 14 '26 03:05

Julien Roncaglia



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!