Click here to Skip to main content
Click here to Skip to main content

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

By , 2 Oct 2011
 

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

supercat9
Web Developer
Unknown
Member
Embedded systems programmer since 1994.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 2 Oct 2011
Article Copyright 2011 by supercat9
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid