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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
VulpesRank #55 · 29.2K reputationPosted Mar 25, 2012, 5:08 AM EDT
In contrast, if a method returns a value, then it returns the actual value, not an address.
SenthilkumarRank #387 · 6.5K reputationPosted Mar 25, 2012, 5:30 AM EDT
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.
Guest UserRank #4,868 · 321 reputationPosted Mar 25, 2012, 7:11 AM EDT
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
VulpesRank #55 · 29.2K reputationPosted Mar 25, 2012, 7:42 AM EDT
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 :)