Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Boxing a value type in .NET to make it a reference type

0.00/5 (No votes)
2 Oct 2011 1  
Mutable structures and classes behave differently when it comes to Generics. Although such differences cause some people to deride mutable structs as evil, they simply offer different semantics from class types. Such semantics are for many purposes superior to those of class types, but some code...

Mutable structures and classes behave differently when it comes to Generics. Although such differences cause some people to deride mutable structs as evil, they simply offer different semantics from class types. Such semantics are for many purposes superior to those of class types, but some code which expects class-type semantics may be broken if given a struct. For example, value-type entities may be implicitly cast to any implemented interface type, ValueType, or Object. Such casts will cause the system to copy the contents of that entity to a new boxed instance; mutations performed upon that new instance will not affect any other. Personally, I think such implicit behavior is evil (there should be easy ways for structs to explicitly permit such implicit behavior, or for code to explicitly request boxing, but such features don't exist as of .NET 4.0).

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here