BOXING & UNBOXING Larry Nung
BOXING & UNBOXING
Larry Nung
AGENDA
Boxing/UnBoxing
Issue for boxing/unboxing
How to find out boxing/Unboxing
2
BOXING/UNBOXING
3
BOXING
Value Type => Object
New object must be allocated and constructed
Expensive computationally
4
int i = 123;
object o = i; // explicit boxing
UNBOXING
Extracts value type from the object
Expensive computationally
5
int i = 123; // a value type
object o = i; // boxing
int j = (int)o; // unboxing
ISSUE FOR BOXING/UNBOXING
6
ISSUE FOR BOXING/UNBOXING
7
using System;
using System.Diagnostics;
internal class Program {
private static void Main(string[] args) {
var count = 1000000000;
Console.WriteLine(“Boxing: {0} ms",
DoTest(count, () => {
var value = 123;
object value2 = value;
}));
Console.WriteLine(“None boxing: {0} ms",
DoTest(count, () => {
var value = 123;
var value2 = value;
}));
}
static long DoTest(int count, Action action) {
var sw = Stopwatch.StartNew();
for (int i = 0; i < count; ++i) action();
return sw.ElapsedMilliseconds;
}
}
0
2000
4000
6000
8000
10000
12000
1000000 10000000 100000000 1000000000
Boxing
None boxing
ISSUE FOR BOXING/UNBOXING
8
using System;
using System.Diagnostics;
internal class Program {
private static void Main(string[] args) {
var count = 1000000000;
Console.WriteLine("Boxing: {0} ms",
DoTest(count, () => {
var value = 123;
var value2 = string.Format("{0}", value);
}));
Console.WriteLine("None Boxing: {0} ms",
DoTest(count, () => {
var value = 123;
var value2 = string.Format("{0}", value.ToString());
}));
}
static long DoTest(int count, Action action) {
var sw = Stopwatch.StartNew();
for (int i = 0; i < count; ++i) action();
return sw.ElapsedMilliseconds;
}
}
0
50000
100000
150000
200000
250000
Boxing
None Boxing
ISSUE FOR BOXING/UNBOXING
9
using System;
internal class Program {
private static void Main(string[] args)
{
try {
var value1 = 1;
object value2 = value1;
var value3 = (float)value2;
} catch (Exception ex) {
Console.WriteLine(ex);
}
}
}
ISSUE FOR BOXING/UNBOXING
10
using System;
internal class Program
{
private static void Main(string[] args)
{
object a = 1;
object b = 1;
bool value = a == b;
bool value2 = a.Equals(b);
Console.WriteLine(value);
Console.WriteLine(value2);
}
}
ISSUE FOR BOXING/UNBOXING
11
using System;
internal class Program
{
private static void Main(string[] args)
{
object a = 1;
object b = 1;
bool value = a == b;
bool value2 = a.Equals(b);
Console.WriteLine(value);
Console.WriteLine(value2);
}
}
ISSUE FOR BOXING/UNBOXING
12
using System;
struct Counter
{
private int x;
public void Increment() { this.x++; }
public int Count { get { return this.x; } }
}
internal class Riddle
{
public readonly object counter = new Counter();
}
internal class Program
{
private static void Main(string[] args)
{
var riddle = new Riddle();
((Counter)riddle.counter).Increment();
Console.WriteLine(((Counter)riddle.counter).Count);
}
}
ISSUE FOR BOXING/UNBOXING
13
using System;
struct Counter
{
private int x;
public void Increment() { this.x++; }
public int Count { get { return this.x; } }
}
internal class Riddle
{
public readonly object counter = new Counter();
}
internal class Program
{
private static void Main(string[] args)
{
var riddle = new Riddle();
((Counter)riddle.counter).Increment();
Console.WriteLine(((Counter)riddle.counter).Count);
}
}
HOW TO FIND OUT
BOXING/UNBOXING
14
ILDASM
15
ILSPY
16
.NET REFLECTOR
17
BOXCOP
For .NET 3.5
18
RESHARPER - HEAP ALLOCATION VIEWER
EXTENSION
19
RESHARPER - HEAP ALLOCATION VIEWER
EXTENSION
20
RESHARPER - HEAP ALLOCATION VIEWER
EXTENSION
21
CLR C# HEAP ALLOCATION ANALYZER
For Visual Studio 2015
22
REFERENCE
Boxing and Unboxing (C# Programming Guide)
https://msdn.microsoft.com/en-
us//library/yz2be5wk.aspx?f=255&MSPPError=-
2147217396
[C#]Effective C# 條款十七:盡量減少裝箱與拆箱 -
Level Up-點部落
http://www.dotblogs.com.tw/larrynung/archive/2011/03/0
8/21712.aspx
[.NET Resource]透過BoxCop偵測程式是否存在Boxing與UnBoxing - Level Up-點部落
http://www.dotblogs.com.tw/larrynung/archive/2013/03/0
1/94727.aspx 23
ReSharper - Heap Allocation Viewer Extension -
Level Up
http://larrynung.github.io/2014/08/12/resharper-heap-
allocation-viewer-extension/
Clr C# Heap Allocation Analyzer - Level Up
http://larrynung.github.io/2014/11/05/clr-c-number-heap-
allocation-analyzer/
24
QUESTION & ANSWER
25