BEST PRACTICES Boxing and unboxing

Boxing and unboxing incur overhead, so you should avoid them when programming intensely
repetitive tasks. Boxing also occurs when you call virtual methods that a structure inherits from
System.Object, such as ToString. Follow these tips to avoid unnecessary boxing:
■ Implement type-specific versions (overloads) for procedures that accept various value types.
It is better practice to create several overloaded procedures than one that accepts an Object argument.
■ Use generics whenever possible instead of accepting Object arguments.
■ Override the ToString, Equals, and GetHash virtual members when defining structures.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s