Prev | Current Page 580 | Next

Guy Fouch?©, Trey Nash

"Accelerated VB 2008"


Value-Type Best Practices
While investigating the notions of canonical forms for value types, you??™ll find that some of the
concepts that apply to reference types may be applied here as well. However, there are notable
differences. For example, it makes no sense to implement ICloneable on a value type. Technically,
you could, but since ICloneable returns an instance of type Object, your value type??™s implementation
of ICloneable.Clone() would most likely just be returning a boxed copy of itself. You can get
the exact same behavior by simply casting a value type instance into a reference to System.Object,
as long as your value type doesn??™t contain any reference types. In fact, you could argue that value
types that contain mutable reference types are bordering on poor design. Value types are best
used for immutable, lightweight data chunks. As long as the reference types your value type contains
are immutable??”similar to System.String, for example??”you don??™t have to worry about
implementing ICloneable on your value type. If you find yourself being forced to implement
ICloneable on your value type, take a closer look at the design. It??™s possible that your value type
should be a reference type.
Value types don??™t need a finalizer, and, in fact, VB won??™t let you create a finalizer on a
structure.


Pages:
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
zwierzęta hodowlane Praca w Niemczech śmieszne dowcipy bajka dieta light