Hi Guys
NP109 Correct or Not
In “C# Heap(ing) Vs Stack(ing) in .NET: Part II” article under the Passing Reference Types heading I have a confusion. Article is saying “we get the value "12345".
In my view we will not get “12345” but “2”. Because ref has not been used in the associated program, please tell me whether I am correct or not.
Thank you
Posted Jul 27, 2008, 11:00 AM
Thank you for your explanation.
AlanPosted Jul 27, 2008, 10:48 AM
The article is in fact correct - you will get "12345" because MyInt is a reference type not a value type.
Try it and see:
using System;
public class MyInt
{
public int MyValue;
}
class Test
{
static void Main()
{
Test t = new Test();
t.Go();
Console.ReadKey();
}
public void Go()
{
MyInt x = new MyInt();
x.MyValue = 2;
DoSomething(x);
Console.WriteLine(x.MyValue.ToString());
}
public void DoSomething(MyInt pValue)
{
pValue.MyValue = 12345;
}
}