The main difference between value types and reference types

Possible duplicate:
What are the differences between value types and reference types in C #?

what are the main differences between value types and rewrite types

+4
source share
5 answers

Consider two variables:

SomeReferenceType x; SomeValueType y; 

The value of x is a reference - it will be either null or a reference to an object, which itself is an instance of SomeReferenceType or a derived class. The value of x is not an object in itself.

The y value is the data itself - if SomeValueType has three fields, the y value will contain these fields.

This is a very short summary β€” see Eric Lippert's blog post on value types and in my article for more information. Information. (You may also be interested in my article on parameter passing , which is related, but not quite like that.)

+17
source

The types of values, as indicated in the name, are values ​​stored in memory; referencer types are (kind of) a pointer to an object (class, object, etc.)

From Microsoft :

A data type is a value type if it contains data within its own memory allocation. The type of reference contains a pointer to another memory location in which the data is stored.

Value Types Value types include the following:

  • All numeric data types
  • Boolean, Char and Date
  • All structures, even if their elements are reference types
  • Enumerations, since their base type is always SByte, Short, Integer, Long, Byte, UShort, UInteger, or ULong

Link Types Link types include the following:

  • Line
  • All arrays, even if their elements are value types
  • Class types such as Form
  • Delegates
+7
source

Variables of <reference types called objects store references to actual data; see here . They include classes, interfaces, and delegates.

From MSDN :

Value types are structures and Enumerations. Variables that are based on value types contain values. Assigning one type of value to a variable copies another containing value. This is different from assigning a reference type to variables that copy the reference to the object, but not the object itself. All value types are derived implicitly from System.ValueType . Unlike with reference types, you cannot deduce a new type from a value type. However, as reference types, structs can implement interfaces. Unlike reference types, a value type cannot contain a null value. However, the nullable types function allows the types of values ​​to be assigned null

Read this: http://www.csharptocsharp.com/node/41

+2
source

When you have a value type variable, that variable directly contains the value. If you assign it to another variable, the value will be copied directly. When a variable has a reference type, it does not directly hold the value, but rather a reference (pointer) to the value. When you copy a variable, you are not copying the value that it points to, but a link (pointer).

You can learn more on MSDN: http://msdn.microsoft.com/en-us/library/s1ax56ch.aspx and http://msdn.microsoft.com/en-us/library/490f96s2.aspx

+1
source

Source: https://habr.com/ru/post/1346063/


All Articles