What is the difference between a reference type an

2018-12-31 02:02发布

Some guy asked me this question couple of months ago and I couldn't explain it in detail. What is the difference between a reference type and a value type in C#?

I know that value types are int, bool, float, etc and reference types are delegate, interface, etc. Or is this wrong, too?

Can you explain it to me in a professional way?

14条回答
裙下三千臣
2楼-- · 2018-12-31 02:08

Variable types and Reference Value are easy to apply and well applied to the domain model, facilitate the development process.

To remove any myth around the amount of "value type", I will comment on how this is handled on the platform. NET, specifically in C # (CSharp) when called APIS and send parameters by value, by reference, in our methods, and functions and how to make the correct treatment of the passages of these values​​.

Read this article Variable Type Value and Reference in C #

查看更多
宁负流年不负卿
3楼-- · 2018-12-31 02:13

This is from a post of mine from a different forum, about two years ago. While the language is vb.net (as opposed to C#), the Value Type vs. Reference type concepts are uniform throughout .net, and the examples still hold.

It is also important to remember that within .net, ALL types technically derive from the base type Object. The value types are designed to behave as such, but in the end they also inherit the functionality of base type Object.

A. Value Types are just that- they represent a distinct area in memory where a discrete VALUE is stored. Value types are of fixed memory size and are stored in the stack, which is a collection of addresses of fixed size.

When you make a statement like such:

Dim A as Integer
DIm B as Integer

A = 3
B = A 

You have done the following:

  1. Created 2 spaces in memory sufficient to hold 32 bit integer values.
  2. Placed a value of 3 in the memory allocation assigned to A
  3. Placed a value of 3 in the memory allocation assigned to B by assigning it the same value as the held in A.

The Value of each variable exists discretely in each memory location.

B. Reference Types can be of various sizes. Therefore, they can't be stored in the "Stack" (remember, the stack is a collection of memory allocations of fixed size?). They are stored in the "Managed Heap". Pointers (or "references") to each item on the managed heap are maintained in the stack (Like an Address). Your code uses these pointers in the stack to access objects stored in the managed heap. So when your code uses a reference variable, it is actually using a pointer (or "address" to an memory location in the managed heap).

Say you have created a Class named clsPerson, with a string Property Person.Name

In this case, when you make a statement such as this:

Dim p1 As clsPerson
p1 = New clsPerson
p1.Name = "Jim Morrison"

Dim p2 As Person

p2 = p1

In the case above, the p1.Name Property will Return "Jim Morrison", as you would expect. The p2.Name property will ALSO return "Jim Morrison", as you would Iintuitively expect. I believe that both p1 and p2 represent distinct addresses on the Stack. However, now that you have assigned p2 the value of p1, both p1 and p2 point to the SAME LOCATION on the managed heap.

Now COnsider THIS situation:

Dim p1 As clsPerson
Dim p2 As clsPerson

p1 = New clsPerson
p1.Name = "Jim Morrison"

p2 = p1

p2.Name = "Janis Joplin"

In this situation, You have created one new instance of the person Class on the Managed Heap with a pointer p1 on the Stack which references the object, and assigned the Name Property of the object instance a value of "Jim Morrison" again. Next, you created another pointer p2 in the Stack, and pointed it at the same address on the managed heap as that referenced by p1 (when you made the assignement p2 = p1).

Here comes the twist. When you the Assign the Name property of p2 the value "Janis Joplin" you are changing the Name property for the object REFERENCED by Both p1 and p2, such that, if you ran the following code:

MsgBox(P1.Name)
'Will return "Janis Joplin"

MsgBox(p2.Name)
'will ALSO return "Janis Joplin"Because both variables (Pointers on the Stack) reference the SAME OBJECT in memory (an Address on the Managed Heap). 

Did that make sense?

Last. If you do THIS:

DIm p1 As New clsPerson
Dim p2 As New clsPerson

p1.Name = "Jim Morrison"
p2.Name = "Janis Joplin"

You now have two distinct Person Objects. However, the minute you do THIS again:

p2 = p1

You have now pointed both back to "Jim Morrison". (I am not exactly sure what happened to the Object on the Heap referenced by p2 . . . I THINK it has now gone out of scope. This is one of those areas where hopefullly someone can set me straight . . .). -EDIT: I BELIEVE this is why you would Set p2 = Nothing OR p2 = New clsPerson before making the new assignment.

Once again, if you now do THIS:

p2.Name = "Jimi Hendrix"

MsgBox(p1.Name)
MsgBox(p2.Name)

Both msgBoxes will now return "Jimi Hendrix"

This can be pretty confusing for a bit, and I will say one last time, I may have some of the details wrong.

Good Luck, and hopefully others who know better than me will come along to help clarify some of this . . .

查看更多
伤终究还是伤i
4楼-- · 2018-12-31 02:17

value data type and reference data type

1) value( contain the data directly ) but reference ( refers to the data )

2) in value( every variable has its own copy) but
in reference (more than variable can refer to some objects)

3) in value (operation variable can`t effect on other variable ) but in reference (variable can affect other )

4) value types are(int, bool, float) but reference type are (array , class objects , string )

查看更多
刘海飞了
5楼-- · 2018-12-31 02:19

I found it easier to understand the difference of the two if you know how computer allocate stuffs in memory and know what a pointer is.

Reference is usually associated with a pointer. Meaning the memory address where your variable reside is actually holding another memory address of the actual object in a different memory location.

The example I am about to give is grossly over simplified, so take it with a grain of salt.

Imagine computer memory is a bunch of PO boxes in a row (starting w/ PO Box 0001 to PO Box n) that can hold something inside it. If PO boxes doesn't do it for you, try a hashtable or dictionary or an array or something similar.

Thus, when you do something like:

var a = "Hello";

the computer will do the following:

  1. allocate memory (say starting at memory location 1000 for 5 bytes) and put H (at 1000), e (at 1001), l (at 1002), l (at 1003) and o (at 1004).
  2. allocate somewhere in memory (say at location 0500) and assigned it as the variable a.
    So it's kind of like an alias (0500 is a).
  3. assign the value at that memory location (0500) to 1000 (which is where the string Hello start in memory). Thus the variable a is holding a reference to the actual starting memory location of the "Hello" string.

Value type will hold the actual thing in its memory location.

Thus, when you do something like:

var a = 1;

the computer will do the following:

  1. allocate a memory location say at 0500 and assign it to variable a (the same alias thing)
  2. put the value 1 in it (at memory location 0500).
    Notice that we are not allocating extra memory to hold the actual value (1). Thus a is actually holding the actual value and that's why it's called value type.
查看更多
永恒的永恒
6楼-- · 2018-12-31 02:21

There is not a single difference between value types and reference types, there are many little details that are stated explicitly by the standard and some of them are not easy to understand, especially for beginners.

See ECMA standard 33, Common Language Infrastructure (CLI). The CLI is also standardized by the ISO. I would provide a reference but for ECMA we must download a PDF and that link depends on the version number. ISO standards cost money.

One difference is that value types can be boxed but reference types generally cannot be. There are exceptions but they are quite technical.

Value types cannot have parameter-less instance constructors or finalizers and they cannot refer to themselves. Referring to themselves means for example that if there is a value type Node then a member of Node cannot be a Node. I think there are other requirements/limitations in the specifications but if so then they are not gathered together in one place.

查看更多
呛了眼睛熬了心
7楼-- · 2018-12-31 02:22

Value Type:

  • Fixed memory size.

  • Stored in Stack memory.

  • Holds actual value.

    Ex. int, char, bool, etc...

Reference Type:

  • Not fixed memory.

  • Stored in Heap memory.

  • Holds memory address of actual value.

    Ex. string, array, class, etc...

查看更多
登录 后发表回答