views:

52

answers:

2

Please clarify my understanding 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 ?

+3  A: 

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.

Reed Copsey
I wouldn't worry about RAM/etc. ----------> but i need know whats happening in RAM when do this in both ways :-) other things what u told is already known by me!
I would personally use "value" (or "argument") instead of "object" for the 3rd and 4th paragraphs. I find it's useful to talk about the "object" as the portion of memory where the actual data for an instance of a class (or other value type) resides.
Jon Skeet
@Jon: True... Good point.
Reed Copsey
@SmartestVEGA: It's all about what's being copied. If you read through what I wrote, you will see where memory is copied. When you're working with the CLR, it's difficult/impossible to know whether you're current code is going to be working on RAM, local cache, etc... but you can know when memory is being copied around, which is what is truly important. Details of where that memory is stored is really abstracted away...
Reed Copsey
+1  A: 

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)
C. Ross