当前位置:网站首页>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
 Icon

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 .

原网站

版权声明
本文为[Geek style]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202160403360000.html