(.NET) Boxing and Unboxing
2010-12-11 15:52
375 查看
[b]Boxing and Unboxing[/b]
Most of us have the knowledge about "Boxing" and "Unboxing".
For "Boxing" means convert value type to reference type.
For "Unboxing" means convert reference type to value.
More deeper,
1. For Boxing, CLR did three things in briefly,
(1) Allocate a heap memory.
(2) Copy the value to heap.
(3) Return the pointer of that "Boxed" type.
2. For Unboxing,
(1) Unboxing.
(2) Copy.
Note that, I mentioned that Unboxing and Copy, because,
(1) For Unboxing, it means that get each filed that contained the boxed type. This process is called unboxing.
(2) For Copy, it means copy the value to the value type instance in the stack.
[b]Why boxing and unboxing?[/b]
Consider ArrayList. You may want to add values to the list. But after you dig into the argument of Add method of ArrayList, you found that the parameter is "Object". So, in this case, a boxing is needed. Also, while you are reading the data, you need to convert it to value type, so a unboxing needed.
The disadvantage is, it will impact the performance of your application.
[b]Generic[/b]
One much better way is that us "Generic Type". It has below advantages,
1. Type examine while your compiling.
2. Force to value type if you passed the value type.
3. Stack allocated while running.
4. Less operations in heap memory.
5. Less force convertion happened.
[b]How many boxing are there?[/b]
Now, let's try to see an interesting code, to see how many boxing are there?
A HA. One? Two? Or Three?
The answer is "three", there are three boxings happened.
Why it's three other 1 or 2?
Before we try to find the root cause, let's understand a keyword in IL called "box", which did the "Boxing". So, if you examine your IL code, you will absolutely found the box keyword. See below IL picture,
So, after you counted the box, you will see "3". And you will also notice that there is a Concat method?
Why there is a "Concat" method?
Please guess it by yourself.
Thanks.
Most of us have the knowledge about "Boxing" and "Unboxing".
For "Boxing" means convert value type to reference type.
For "Unboxing" means convert reference type to value.
More deeper,
1. For Boxing, CLR did three things in briefly,
(1) Allocate a heap memory.
(2) Copy the value to heap.
(3) Return the pointer of that "Boxed" type.
2. For Unboxing,
(1) Unboxing.
(2) Copy.
Note that, I mentioned that Unboxing and Copy, because,
(1) For Unboxing, it means that get each filed that contained the boxed type. This process is called unboxing.
(2) For Copy, it means copy the value to the value type instance in the stack.
[b]Why boxing and unboxing?[/b]
Consider ArrayList. You may want to add values to the list. But after you dig into the argument of Add method of ArrayList, you found that the parameter is "Object". So, in this case, a boxing is needed. Also, while you are reading the data, you need to convert it to value type, so a unboxing needed.
The disadvantage is, it will impact the performance of your application.
[b]Generic[/b]
One much better way is that us "Generic Type". It has below advantages,
1. Type examine while your compiling.
2. Force to value type if you passed the value type.
3. Stack allocated while running.
4. Less operations in heap memory.
5. Less force convertion happened.
[b]How many boxing are there?[/b]
Now, let's try to see an interesting code, to see how many boxing are there?
static void Main(string[] args) { Int32 i = 5; object o = i; i = 123; Console.WriteLine(i + "--" + (Int32)o); }
A HA. One? Two? Or Three?
The answer is "three", there are three boxings happened.
Why it's three other 1 or 2?
Before we try to find the root cause, let's understand a keyword in IL called "box", which did the "Boxing". So, if you examine your IL code, you will absolutely found the box keyword. See below IL picture,
So, after you counted the box, you will see "3". And you will also notice that there is a Concat method?
Why there is a "Concat" method?
Please guess it by yourself.
Thanks.
相关文章推荐
- .Net的六个重要概念:Stack, heap, value types, reference types, boxing, and unboxing
- C#: boxing and unboxing
- 6 important .NET concepts: - Stack, heap, Value types, reference types, boxing and Unboxing
- .net中6个重要的基础概念:Stack, heap, Value types, reference types, boxing and Unboxing.
- [转载 收藏]6 important .NET concepts: - Stack, heap, Value types, reference types, boxing and Unboxing.
- C#中装箱和拆箱的作用 the effection of boxing and unboxing
- 6 important .NET concepts: - Stack, heap, Value types, reference types, boxing and Unboxing.
- 6 important .NET concepts: - Stack, heap, Value types, reference types, boxing and Unboxing.
- 11.3 Boxing and unboxing
- C++/CLI Boxing/UnBoxing and Ref/Out Issue
- 6 important .NET concepts: - Stack, heap, Value types, reference types, boxing and Unboxing.
- BOXING AND UNBOXING FOR BEGINNER
- 装箱和拆箱(boxing&unboxing)-.NET
- BOXING AND UNBOXING FOR BEGINNER
- CLR 是怎样去处理Boxing and Unboxing Value Types?
- Boxing and unboxing in C#
- Boxing and Unboxing
- Java Auto Boxing and Unboxing -Java 学习笔记 (23)
- Effective C# Item17: Minimize Boxing and Unboxing
- C#中的装箱和拆箱(boxing and unboxing)