C # difference between value types and reference types

Brief introduction

  A few years ago, when only in school know the value of existence is the type of the stack, the stack there is a reference type, but in the end why the benefits of such storage, and so do not understand what is stored, just this year began recording blog, and read some contents of the book record it.

 

Value Type

Types of Defaults Explanation
byte 0 Unsigned 8-bit integers (0 to 255)
sbyte 0 Signed 8-bit integer (-128 to 127)
long 0 64-bit signed integer (-2
ulong 0 64-bit unsigned integer
short  0 16-bit signed integer
ushort  0 16-bit unsigned integer
int 0 32-bit signed integer
uint  0 32-bit unsigned integer
float  0.0 It represents a single-precision floating point number
double 0.0 It denotes a double precision floating point
decimal  null 28 Accuracy
char '\0' 16 characters
bool  false true or false
struct  null Structure
enum  null Enumerated type

 

1. The value of the implicit type of inherited (System.ValueType), the value of the type which is generally held in the stack, a value of the instance when the type of open space in the stack itself already contains a time field, the performance is than the reference speed type (reference type is stored in the pointing object data memory address), and the value type since not stored in the stack which are not to be gc of control, the value of the type used to alleviate pressure on the managed heap, and decrease garbage recycling times during the lifetime of the application.

 When creating a value type the following, when added to create an instance of the type is 0 num respective stack them. Because these simple type definition if the words created much trouble in accordance with the new syntax, the compiler syntax to give these are optimized can be directly written int num = 0;

public  class Test 
{ 
   int num ; 
}

 

 

 

Reference types

 

Types of Defaults Explanation
class   null class
object null  All types derive from object
string null  String
interface null interface
array null  Array
delegate null  Entrust

1.引用类型的数据都是存放在堆当中,在使用new操作返回的是对象的内存地址,

 

 static void Main(string[] args)
        {
            string str = "测试";
            Console.ReadLine();
        }

 

2.下面我们改变下str的值,引用类型不会去改变已经存在内存当中的数据,如果现在我们把str的值改为一个“test”,实际上他会在堆当中创建一个新的内存区域,并且把str的指针指向新创建的这个对象,原来的那个数据“测试”,在经过gc扫描的时候,发现并没有任何引用的话,gc会释放掉。

 static void Main(string[] args)
        {
            string str = "测试";
             str="Test";
            Console.ReadLine();
        }

3.有的时候在开发的时候,我们分不清楚为什么要给string类型初始化的时候设置string.Empty,而不是""呢,那是因为string.empty只是创建了一个空的对象,但是""的话是去内存开辟了相应的空间,所以如果一个在开始不清楚变量值得时候,可以尽量采用string.Empty来创建。       

static void Main(string[] args)
        {
            string str =string.Empty;
            string st="";
            Console.ReadLine();
        }

 总结

值类型:

  1.  栈中存储具体的值,减少缓解了托管堆的压力,并且减少了应用程序生存期内的垃圾回收次数。
  2. 值类型在被当做参数传递的时候,实际上是对值类型的字段进行复制,对性能有所影响。
  3. 值类型不一定都存在栈中,如果一个类(class)当中包含了值类型(int num),那么这个num实际上是存储在堆上的。
  4. 值类型都是密封类

引用类型:

  1. 引用类型存在堆当中,new 获取的实际上是指向对象的内存的地址。
  2. 引用类型当做参数传递的时候,传递的是引用的地址,而不是引用。
  3. 将引用的变量赋值给另一个变量,实际上是复制的内存地址,两个变量指向的相同的对象。
  4. 引用类型可以派生新类型。

Guess you like

Origin www.cnblogs.com/zhengyazhao/p/11371691.html