什么是C#值类型和引用类型之间的区别? [英] What are the differences between value types and reference types in C#?

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

问题描述

我知道有一些差别,


  1. 值类型的存储位置为引用类型存储在托管堆中的堆栈。

  2. 值类型变量直接包含其值,其中作为参考变量只能容纳到在托管堆上创建的对象的位置的参考。

有没有其他的区别我错过了......如果是这样,它们是什么?

Is there any other difference i missed... If so,what are they?

推荐答案

请阅读:<一href=\"http://blogs.msdn.com/ericlippert/archive/2009/04/27/the-stack-is-an-implementation-detail.aspx\">The堆栈是一个实现细节,不要再不断重复堆栈分配是与引用类型的区别.NET值类型的鸭式布局。在CLR可以选择在任何地方分配变量就是了。

Please read: The stack is an implementation detail, and don't ever again repeat the canard that stack allocation is what differentiates value types from reference types in .NET. The CLR may choose to allocate a variable anywhere it wants to.

最重要的区别是在分配语义。当你分配一个值类型变量(或将它传递给作为参数的方法),所有数据将被复制。当您将引用类型,只是一个参考复制 - 两个引用指向同一个对象实例在内存

The most important difference is in the assignment semantics. When you assign a value type to a variable (or pass it to a method as an argument), all of the data is copied. When you assign a reference type, only a reference is copied - both references point to the same object instance in memory.

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

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