值和引用类型 [英] Value and Reference types

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

问题描述

值类型和引用类型之间的区别是什么?它在编程中有何意义?我对面向对象的编程范例是全新的!举一个例子的解释会很有帮助....

在此先感谢!!

What is the difference between Value and Reference Types and its Significant in programming ? I''m totally new to object oriented programming paradigm ! An explanation with an example would be much helpful....

Thanks in Advance !!

推荐答案

数字数据类型,例如整数,浮点数,布尔值,枚举和用户定义的结构都是值类型.
类,接口和委托是引用类型.

引用类型的实例(称为对象)在垃圾收集堆上分配和管理,并且对它的所有读取,写入和共享都是通过引用(即指针间接寻址)执行的.

另一方面,一个称为值的值类型实例被内联分配为一个字节序列,其位置基于定义它的范围
Numeric data types such as integers, floats, etc, boolean, enumerations and user defined structures are value types.

Classes, interfaces and delegates are reference types.

An instance of the reference type, called an object, is allocated and managed on the Garbage Collection heap, and all reads, writes, and sharing of it are performed through a reference (i.e., a pointer indirection).

A value type instance, called a value, on the other hand, is allocated inline as a sequence of bytes, the location of which is based on the scope in which it is defined


hi dude ,

基本上,数据类型可以分为两种基本类型.

1)值类型
2)引用类型.


值类型直接保存其值,但引用类型不保存.

hi dude,

Basically data types can be classified into two basic types.

1)Value type
2)Reference type.


value types directly holds its value but reference type not.

int myVar = 10;



当上述语句执行时,CLR分配一个地址为"myVar"的堆栈.堆栈"myVar"将保留您的值10.



when the above statement executes, CLR allocates a stack with address "myVar". The stack "myVar" will hold your value 10.

String myvar ="Hello world";



当上述代码执行时,CLR将分配一个地址为"myvar"的堆栈,但不会直接保存该值("hello world").代替tis,CLR会将实际值("hello world")存储在堆中,并将堆地址存储在堆栈"myVar"中.



When the above code executes, CLR will allocates a stack with address "myvar" but it will not directly holds the value("hello world"). instead of tis, CLR will store the actual value("hello world") in heap and stores the heap address in the stack "myVar".


当很多人解释说非常详细的方式,为什么我们要重复这些话.因此,花一些时间阅读这些资源:

http://msdn.microsoft.com/en-us/library/0f66670z%28v = vs.71%29.aspx [ ^ ]

http://www.developerfusion.com/article/4697/parameter-passing-in-c / [^ ]

http://www.albahari.com/valuevsreftypes.aspx [
When a lot of people have explained that in very detailed manner why should we repeat those words. So spend some time and read these resources :

http://msdn.microsoft.com/en-us/library/0f66670z%28v=vs.71%29.aspx[^]

http://www.developerfusion.com/article/4697/parameter-passing-in-c/[^]

http://www.albahari.com/valuevsreftypes.aspx[^]

Hope it helps.


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

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