当前位置:网站首页>Packing and unpacking of C #
Packing and unpacking of C #
2022-07-01 01:05:00 【Geek style】
One 、 Packing and unpacking concepts
Packing : Converting a value type to a reference type is called boxing
Unpacking : Converting a reference type to a value type is called unpacking
1、 stay C# in , Value type data is stored on the stack (stack) On , Data of reference type is stored in the heap (heap) On , Only the first address of the data in the heap memory is saved on the stack
using Ststem.Collections;
using System.Collections.Generic;
using UnityEngine;
class Animal
{
public int age;
public Animal(int age)
{
this.age = age;
}
}
public class s1 : MonoBehaviour {
void Start () {
int x = 3;
Vector3 v = new Vector3(1,2,3);
Animal a = new Animal(3);
void Update () {
}
}
The above paragraph is in Unity Medium C# Code , stay Start() Three variables are defined in , They are integers x、 Vector type v、Animal type a. this 3 Initialization method of type in , On the face of it ,v and a It's the same , It's all used new Operator to initialize variables , Variables x It is directly assigned . But actually , In the way data is stored , Instead, it's x and v It's the same , They are all value types stored on the stack , And only Animal a Is the reference type stored on the heap 
2、 Time for packing and unpacking
using Ststem.Collections;
using System.Collections.Generic;
using UnityEngine;
class Animal
{
public int age;
public Animal(int age)
{
this.age = age;
}
}
public class s2 : MonoBehaviour {
void Start () {
int x = 12;
print(x);
print(new Vector3(1,1,1));
print("Metaverse");
print(new Animal(3));
void Update () {
}
It mainly uses print The way to console Windows print different types of data . First print() The parameter of the method is a object type ,object Type is the parent of all other types , So any one C# All variables in meet is a objcet The relationship between , therefore print Method can be used to print all types of data .
The first 1 individual print Statements are used to print x Variable ,x It's a int Data of type , This call will int Type conversion to object type , Therefore, it is to convert the value type to the reference type , Belongs to packing .
The first 2 individual print Statement to print a vector , stay Unity in ,Vector Yes, it is struct Defined , Therefore, it belongs to value type , Here is still the conversion from value type to reference type .
The first 3 individual print The statement prints a Animal Variable of type , use class Definition Animal, So it's a reference type . here take Animal Type conversion to object A type is a conversion of a type , Instead of packing .
Two 、 summary
1、 A common scenario is to call a containing type Objcet The parameter method of , The Object Any type of , In order to be universal . When you need to pass in a value type , Need to be boxed .
2、 A non generic container , It's also for general purpose , The element type is defined as Objcet. therefore , To add value type data to the container , Need to be boxed .
When boxing, a new reference object is generated , There will be time lost , Resulting in reduced efficiency , So try to avoid packing . The two situations mentioned above , Can be avoided , In the 1 In this case , You can avoid by overloading functions , And the first 2 This can be avoided by generics .
边栏推荐
- 【原创】 PLSQL 索引排序优化
- Oracle table creation and management
- [2023 MediaTek approved the test questions in advance] ~ questions and reference answers
- Introduction to ES6 promise, new features of ES7 and es8 async and await
- PyTorch安装并使用gpu加速
- 2022-2028 global retro glass industry research and trend analysis report
- Join table query select generation
- Tibetan poem PTA
- CTF tool (1) -- archpr -- including installation / use process
- 剑指 Offer 18. 删除链表的节点
猜你喜欢

Oracle临时表详解
![[daily record] - bug encountered in BigDecimal division operation](/img/82/3105586841076b9bb1c57eac221ddf.png)
[daily record] - bug encountered in BigDecimal division operation

20220215 misc buctf easycap Wireshark tracks TCP flow hidden key (use of WinHex tool)

2022-2028 global rampant travel industry research and trend analysis report

C language file operation for conquering C language

PHP online confusion encryption tutorial sharing + basically no solution

CMU15445 (Fall 2019) 之 Project#1 - Buffer Pool 详解

From January 11, 2007 to January 11, 2022, I have been in SAP Chengdu Research Institute for 15 years

Bugku CTF daily one question dark cloud invitation code

High quality pump SolidWorks model material recommended, not to be missed
随机推荐
CentOS install MySQL
Left join displays the specified value when the left join matching data is null
Double linked list: initialize insert delete traversal
Error 2059 when Navicat connects to MySQL
第53章 从业务逻辑实现角度整体性理解程序
Length of the longest integrable subarray
A single element in an ordered array
C # generates PPK files in putty format (supports passphrase)
对libco的一点看法
2022-2028 global rampant travel industry research and trend analysis report
Confirm() method of window
PyTorch安装并使用gpu加速
合适的工作就是好工作
优质的水泵 SolidWorks模型素材推荐,不容错过
Bugku CTF daily one question dark cloud invitation code
Wechat official account development (1) introduction to wechat official account
Plot size and resolution with R markdown, knitr, pandoc, beamer
Day31-t1380-2022-02-15-not answer by yourself
Oracle table creation and management
Cmu15445 (fall 2019) project 1 - buffer pool details