Resolving doubts about values ​​and reference types

value type variables contain actual data directly, and reference variable types contain a link to actual data.

I think of it as:

lhs is the value type, and rhs is the reference type

enter image description here

on the left side, if I copy i to j , the new memory cell will be filled with the same source data (45).

on the right side, if I copy k to l , the new memory cell is populated with an object reference; and this link points to the actual object in memory.

I am now confused by this copying of the reference type. here is a slightly different thing:

enter image description here

Here the copy on rhs does l points to the same place as k .

My question is 1. "Which one is more believable?" or is there something more than I imagined?

In addition, value types can be allocated on the heap, depending on how the jitter sees that it fits, and then 2. Can we bind the reference type to the stack?

Sorry for sloppy image editing.

+4
source share
7 answers

The first image is better, l and k are different variables that occupy different places in memory.

types of values ​​can be allocated on the heap, depending on how jittering it sees that it fits

Actually, it depends more on the context and how the value is used. The value-type field will always be allocated on the heap, boxing and closures are other reasons.

However, the second image applies when l is the ref parameter:

 MyClass k = new ...; M(ref k); void M(ref MyClass l) { /* Here l is an alias for k */ } 

then 2. Can we force a reference type on the stack?

There is something like stackalloc, but it is an optimization that is "invisible" to a C # programmer.
Simple and useful answer: None.

+2
source

Neither one nor the other. The problem is that you are talking about implementation details that are not specified in the C # language itself.

In fact, you can program on a machine that has only a stack, or you may have registers available. At the end of the day, this is just an implementation detail. The model that most resembles reality depends on the architecture of the machine you are working on.

+2
source
  1. Can a link type be pushed onto the stack?

We cannot force anything this way, the link is on the stack when you initialize the variable inside the method, for example. the reference type , in fact, an object initialized with the new keyword, including the value types inside it, is allocated on the heap.

Although this is a topic that you can write a book about, it comes down to the following:

We have two types of type behavior in .NET. Value behavior and reference behavior. The difference between them lies in their concept. The value represents the value itself, the actual data and references are memory cells. Memory locations that are addresses in actual instances of the object created on the heap. They represent a kind of link to the actual object in the virtual address space.

I wrote a blog post that is somewhat more detailed ... and is trying to conceptually explain how it all works at a lower level. But my explanation there is mainly based on x86 architecture and not quite exactly how everything is implemented. How C # and the .NET Framework along with JIT can change, but I hope this helps to make it more understandable.

+2
source

Nice drawing! I think the first image is "more correct". They reference the same object, but they also need variables to store links. References or pointers are also variables, which means they have their own memory location. 2. I do not think so. (I'm not sure)

+1
source
  • the first is the most correct. the link is the number, and there, when you copy, you copy the address. in c you could put the link in an integer, in c # look at you more.

  • if you declare a structure instead of a class, it sits well on the stack (as far as I remember)

0
source

The first image is the most correct because you consider

 l = k; 

If instead you have this situation

 class MyClass { internal string k; internal void Test() { OtherClass.Method(ref k); } } class OtherClass { internal static Method(ref string l) { // do a lot of stuff using l } } 

then in this case I would say that the second image is more correct, because this parameter has the ref keyword. This means that if someone changes the link k to point to another string object , and OtherClass.Method works , then unexpectedly the variable l will point to the new object too.

But this is only true because of the ref keyword.

0
source

The first photo is definitely ... Otherwise, a call with the argument "by-ref" will affect both links, i.e. Xxx (ref k); possibly "redirecting" both k and l.

Regarding the stack and others, you can read Eric Lippert ( The Stack Is An Implementation Details )

0
source

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


All Articles