当前位置:网站首页>Explanation of C value type and reference type
Explanation of C value type and reference type
2022-07-29 09:32:00 【Dotnet cross platform】
To understand value types and reference types , We first need to know the difference between heap and stack :
① Stack is the memory space allocated during compilation , So you have to have a clear definition of the stack size in your code ; Heap is the memory space allocated dynamically during the program running , You can determine the size of heap memory to allocate according to the operation of the program
② When it is stored in the stack, it should take care of the storage order , Keep the principle of "first in, last out" , It is a contiguous memory field , Automatically assigned and maintained by the system .
And the heap is disordered , It is a discontinuous memory field, which is controlled and released by the user , If the user does not release , When memory reaches a certain value , Through the garbage collector (GC) To recycle .
C# The value types of are : Structure , enumeration , Null type ,byte,short,int,long,float,double,decimal,char,bool ; Value types are stored on the data stack ;
C# The reference types of include : Array , User defined classes 、 Interface 、 entrust ,object, character string , Reference types are stored on the heap .
After understanding the above differences , Let's show the difference in use through examples :
Examples of reference types : Here are two definitions byte Array , Exchange data with consistent subscripts :
The code is as follows :
static void Main(string[] args) { byte[] a = new byte[] { 1, 2, 3, 4, 5 }; byte[] b = new byte[] { 6, 7, 8, 9, 10 }; ChangeData(a, b); Console.Write($"a The value of the array :"); for (int i=0;i< a.Count();i++) { Console.Write($"{a[i]} "); } Console.WriteLine("\n ***********"); Console.Write($"b The value of the array :"); for (int i = 0; i < b.Count(); i++) { Console.Write($"{b[i]} "); } Console.ReadKey(); } private static void ChangeData(byte[] aaa, byte[] bbb) { int tempa = aaa.Count(); int tempb = bbb.Count(); int count = tempa > tempb ? tempb : tempa; byte temp = 0; for (int i = 0; i < count; i++) { temp = aaa[i]; aaa[i] = bbb[i]; bbb[i] = temp; } }Running results :

Although we put byte Array passed to method ChangeData To deal with , But after processing a,b Data exchange is realized , Because arrays are reference types , So in the example above aaa and a All point to the same segment of address , So a aaa After change ,a The value of the array has changed .
Examples of value types :
static void Main(string[] args)
{
//byte[] a = new byte[] { 1, 2, 3, 4, 5 };
//byte[] b = new byte[] { 6, 7, 8, 9, 10 };
//ChangeData(a, b);
//Console.Write($"a The value of the array :");
//for (int i=0;i< a.Count();i++)
//{
// Console.Write($"{a[i]} ");
//}
//Console.WriteLine("\n ***********");
//Console.Write($"b The value of the array :");
//for (int i = 0; i < b.Count(); i++)
//{
// Console.Write($"{b[i]} ");
//}
int a = 5;
int b = 8;
ChangeValue(a,b);
Console.WriteLine($"a Value :{a}");
Console.WriteLine($"b Value :{b}");
Console.ReadKey();
}
//private static void ChangeData(byte[] aaa, byte[] bbb)
//{
// int tempa = aaa.Count();
// int tempb = bbb.Count();
// int count = tempa > tempb ? tempb : tempa;
// byte temp = 0;
// for (int i = 0; i < count; i++)
// {
// temp = aaa[i];
// aaa[i] = bbb[i];
// bbb[i] = temp;
// }
//}
private static void ChangeValue(int aaa, int bbb)
{
int temp = aaa;
aaa = bbb;
bbb = temp;
} Running results :

Let's see after the value exchange aaa and bbb The value has changed. , The original a and b The value of has not changed .
边栏推荐
- [machine learning] logistic regression code exercise
- OpenCV图像处理基础操作
- First order traversal / second order traversal determines the approximate shape of the tree
- 怎么样的框架对于开发者是友好的?
- 工业测控设备内生信息安全技术研究综述
- Use cpolar to publish raspberry pie web pages (improvement of cpolar function)
- 当 update 修改数据与原数据相同时会被更新吗?
- 基于C语言模拟实现DFA识别字符串
- Flutter文本编辑器
- Four types of technical solutions shared by distributed sessions, and their advantages and disadvantages
猜你喜欢

怎么样的框架对于开发者是友好的?

存算一体与存内计算计算杂谈

First order traversal / second order traversal determines the approximate shape of the tree

用户身份标识与账号体系实践

Zhongang Mining: four steps for sustainable and healthy development of fluorite industry
![[Apple Developer account]06 after transferring the developer account, the annual fee of the developer is automatically renewed](/img/a7/12fd63f16ebca81a3dd2d1b97847d1.png)
[Apple Developer account]06 after transferring the developer account, the annual fee of the developer is automatically renewed

云原生管理实践:业务引领的DevOps持续交付体系

dataframe.to_sql() 一次性插入过多报错

VS2015采用loadlibrary方式调用dll库

Network security (6)
随机推荐
Floweable advanced
Unity Xchart3.0基本用法快速上手
高智伟:数据管理赋能交通行业数字化转型
View port occupancy
How to introduce your project experience?
I don't know how lucky the boy who randomly typed logs is. There must be a lot of overtime
Redis series 3: highly available master-slave architecture
User identity identification and account system practice
怎么样的框架对于开发者是友好的?
How to change MySQL into Chinese
Flowable 基础篇1
redis可视化工具读取数据乱码问题解决
Opencv introductory basic learning
[Bert multi label text classification practice] I - overview of practical projects
【机器学习】逻辑回归代码练习
36. JS animation
Four types of technical solutions shared by distributed sessions, and their advantages and disadvantages
Could not receive a message from the daemon
《UnityShader入门精要》总结(2):初级篇
AxureRP原型设计 快速开始