Hi
What is the difference in returning an Object and returning a Value please.
Thanks
Steven
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VulpesPosted Mar 25, 2012, 9:08 AM
In contrast, if a method returns a value, then it returns the actual value, not an address.
VulpesPosted Mar 25, 2012, 11:42 AM
The basic difference between a variable of a reference type and a variable of a value type is that the former holds an object reference (i.e. a pointer) and the latter holds the actual value itself.
However, confusing terminology abounds in programming and you'll often see what is being assigned to a variable referred to as a 'value' even though it is in fact an object reference!
The designers of C# haven't helped by using the name 'value' for the implicit parameter used in a property 'set' regardless of whether the property type is a reference type or a value type - though I suppose it might have been even more confusing if they'd used different names for each type :)
Guest UserPosted Mar 25, 2012, 11:11 AM
Given the below example which you kindly commented on in another thread of mine.
Would the Return Value be the Object Reference that will be stored?
// Otherwise invoke the GetCustomersWithOrderStatistics method and store the return value in the customers local variable instead.
Thanks
Steven
SenthilkumarPosted Mar 25, 2012, 9:30 AM
Lets see from the basics.
object obj = "10"; //Derived from the System.Object type
When the object is created, it will be stored in the heap memory. It is object type and it will have the memory address of heap and when you pass to the other method, it passes the actual address not the actual value. When you change in one location in the current context throughout the reference will be changed.
int i = 5; //Derived from the System.ValueType;
The value type will be stored in the stack and it will have the value in the actual memory location. When you pass to a method, it will pass the actual value.
For example,
object obj = "10";
Swap(obj);
public void Swap(object obj1)
{
obj1 = "20";
}
When you pass the object it will send the actual address and when you reassign the object value in the method it will change the value in the declaration variable of "obj".
The object, delegate, string are the example of object type (reference type)
The int, float, enum, struct are the example of value type (stack)
I hope you this will help you to understand.