The Two Types Offered by the CLR: Reference Types and Value Types
The CLR supports two kinds of object types: reference types and value types. Of the two, you will run into reference types much more often. Reference types are always allocated from the managed heap and the new operator returns the memory address of the object – the memory address refers to the object’s bits. There are some performance considerations to consider when working with reference types. First, the memory must be allocated from the managed heap, which could force a garbage collection to occur.
The CLR also supports “lightweight” types called value types. Value type objects are usually allocated on a thread’s stack (although they can be embedded in a reference type object) and the variable representing the object does not contain a pointer to an object; the variable contains the value of the object itself. Since the variable contains the object’s value, a pointer does not have to be dereferenced in order to manipulate the object. Value type objects do not come under the control of the garbage collector thus reducing pressure in the managed heap and reducing the number of collections required by an application over its lifetime.
The .NET Framework Reference documentation clearly indicates which types are reference types and which types are value types. When looking up a type in the documentation, any type called a "Class" is a reference type. For example, the Object Class, the Exception Class, the FileStream Class, and the Random Class are all reference types. On the other hand, the documentation refers to each value type as a "Structure". For example, the Int32 Structure, the Boolean Structure, the Decimal Structure, and the TimeSpan Structure are all value types.
When possible, you should use value types instead of reference types because your application’s performance will be better. In particular, you should declare a type as a value type if all of the following are true:
• The type acts like a primitive type
• The type doesn’t need to inherit from any other type
• The type will not have any other types derived from it
• Objects of the type are not frequently passed as method arguments since this would cause frequent memory copy operations hurting performance.
Understanding Boxing and Unboxing
Value types are lighter-weight than reference types because they are not allocated in the managed heap, not garbage collected, and not referred to by pointers. However, there are many cases where you must get a pointer to a value type object. For example, if you want to add the integer 5 to an ArrayList, then your code would look like this:
參考答案:由统计复用两种: 参考价值类型的种类和支持统计复用两种对象类型:类型和参考价值类型. 上述两个,你会更常碰到的参考类型. 参考类型总是分配由新经营效益和管理堆内存地址对象 -内存地址所指的对象的比特. 有些工作绩效考量,考虑当参考类型. 首先,必须分配内存从管理堆,这可能迫使垃圾发生. 还支持统计复用的"轻量级"类商品种类称为. 通常价值型物体分配一个线程的栈(虽然他们可以参考嵌入型物体) 代表和可变对象不包含指针的对象; 可变物体本身含有价值. 自变量包含对象的价值 指不须dereferenced为了操纵物体. 价值型物体不受到控制的压力大大减轻了拾荒者的管理堆 而收藏所需人数降低其寿命超过申请. <. NET框架参考文献清楚地表明这是种类型,其中类型是参考价值类型. 当仰视型的文件,任何类型称为"级"是指类型. 例如,对象类,除了在课堂上filestream阶级、阶层都是随机参考类型. 在另一方面,该文件是指每一个价值型"结构". 例如,int32结构、布尔结构、小数结构、商品种类都timespan结构. 如果可能的话,你应该使用类型而不参考价值,因为你的应用类型的表现会更好. 特别要申报作为一种价值型若以下全部属实: ●行为型原始型●喜欢的类型不需要继承自任何其他类型次 电子类不会有任何其他类型的对象从它的种类●不经常通过 因为这会引起争议的方法频繁内存拷贝操作伤害表现. 拳击、unboxing认识价值类型的打火机体重比参考类型,因为他们不是在设法拨出堆、 不是垃圾,不要提及的指点. 不过,有许多情况下,你一定要以价值型指针对象. 举例来说,如果你想要添加一个整数5arraylist,那么你的代码将像这样: