值类型和引用类型之间有什么区别 [英] What is difference between value type and reference type

查看:108
本文介绍了值类型和引用类型之间有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我价值类型和参考类型之间有什么区别。



我尝试了什么:



我尝试了解值类型和引用类型之间的区别。

Can someone tell me what is difference between value type and reference type.

What I have tried:

I have tried uderstanding difference between value type and reference type.

推荐答案

Charles Petzold撰​​写的.NET Book Zero [ ^ ]。还有很多其他地方。
Clearly explained in .NET Book Zero by Charles Petzold[^]. And, many other places.


嗨Aliya我想推荐一些有用的链接。我希望它能帮到你。



值类型和参考类型之间的区别 [ ^ ]



< a href =https://www.mindstick.com/blog/190/difference-between-value-type-and-refernce-type>值类型和引用类型之间的差异。 [ ^ ]





C#采访问题TopicWise:值类型和参考类型 [ ^ ]
Hi Aliya i want to refer some useful link.I hope it will help you.

Difference between a Value Type and a Reference Type[^]

Difference between value type and refernce type.[^]


C# Interview Questions TopicWise: Value Type and Reference Types[^]


值类型:



值类型将其内容存储在堆栈上分配的内存中。创建值类型时,将分配内存中的单个空间来存储值,并且该变量直接保存值。如果将其分配给另一个变量,则直接复制该值,并且两个变量都独立工作。预定义的数据类型,结构,枚举也是值类型,并以相同的方式工作。值类型可以在编译时创建并存储在堆栈内存中,因此,垃圾收集器无法访问堆栈。



Ex: -
Value Type:

A Value Type stores its contents in memory allocated on the stack. When you created a Value Type, a single space in memory is allocated to store the value and that variable directly holds a value. If you assign it to another variable, the value is copied directly and both variables work independently. Predefined datatypes, structures, enums are also value types, and work in the same way. Value types can be created at compile time and Stored in stack memory, because of this, Garbage collector can't access the stack.

Ex:-
<pre lang="C#">int x = 10;</code></pre><br />
Reference Type:<br />
<br />
Reference Types are used by a reference which holds a reference (address) to the object but not the object itself. Because reference types represent the address of the variable rather than the data itself, assigning a reference variable to another doesn't copy the data. Instead it creates a second copy of the reference, which refers to the same location of the heap as the original value. Reference Type variables are stored in a different area of memory called the heap. This means that when a reference type variable is no longer used, it can be marked for garbage collection. Examples of reference types are Classes, Objects, Arrays, Indexers, Interfaces etc.<br />
<br />
e.g.<pre lang="C#">int[] iArray = new int[20];</pre>


这篇关于值类型和引用类型之间有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆