开发者

Please clarify my understanding regarding object and reference and value type is current?

开发者 https://www.devze.com 2022-12-21 13:11 出处:网络
Please clarify my understa开发者_如何学Cnding regarding object and reference and value type is current?

Please clarify my understa开发者_如何学Cnding regarding object and reference and value type is current?

Object means a memory location in RAM where we allocate memory while executing a program

Reference means a location(address) in the memory.

Passing by reference means - we are passing or pointing a memory location to the function to take the values for the function from which the memory address has been passed.

Passing by value means we are not giving any address we are giving actual value to a function. Where is this value comes from RAM? is it not touching ram at the time of execution? It is comming only from another memory location..but what is difference in term of executions in the visual context? how i can imagine ?


I wouldn't worry about RAM/etc.

The main difference here, conceptually, is what you're passing into a method.

When you are passing by reference (ref or out in C#), you're passing the location in memory of the original object.

When you are passing by value, you are copying the actual variable's value, and the method receives a complete copy, by value, of the variable that was used in the previous call stack.

If the variable is a reference type, the value being passed is a reference to an object, which is basically a memory location. The reference is copied into the method.

If the object in question is a value type, the entire object is copied, and the method receives a copy of the object.


An object is an instance of a class. The memory for the object, and any members is stored on the heap. In general this is called a [Reference Type][2], meaning that it is handled as a reference (pointer) to a location.

In contrast a struct is a Value Type which is handled directly.

Passing an item By Reference (the ref keyword, or out keyword as Reed points out) passes a reference (pointer) to the item in question. This means that if you are passing an object by reference you are passing a pointer to a pointer to an object in memory.

Object

    arg(stack) -> oRef(heap) -> oData(heap)

This means that we can change the pointer to another place (i.e. an entirely different object).

Passing an item By Value means passing the item in question itself. In the case of a struct, the whole thing is passed on the stack. In the case of an object, the reference is passed on the stack. This means when an object is passed, the object can still be acted on and it's members modified, but it cannot be entirely replaced by another object.

    arg(stack) -> oData(heap)
0

精彩评论

暂无评论...
验证码 换一张
取 消