//The stack is always used to store the following two things
// 1> The reference portion of reference-typed local variables and parameters
// 2> Value-typed local variables and method parameters
//The following data is stored on the heap:
// 1> The content of reference-type objects.
// 2> Anything structured inside a reference-type object.
public class A
{
private int myVar;
public int MyVar
{
get { return myVar; }
set { myVar = value; }
}
}
public class Test1
{
public void TestMain()
{
//we create a local variable that references an object.
//The local variable is stored on the stack,
//while the object itself is stored on the heap
A a = new A();
a.MyVar = 0;
//In C#, parameters are (by default) passed by value,
//meaning that they are implicitly copied when passed to the method
Pass(a);
}
public void Pass(A x)
{
//at this line x and 'a' still point to the same object
//So this will be change on 'a.MyVar' to 9
x.MyVar = 9;
//Even this No effect on 'a' since x is a copy
x = null;
A b = new A();
x = b;
//at this line x and 'a' point to different object
//So this will be NO change on 'a.MyVar'
x.MyVar = 1;
}
}
public class Test2
{
public void TestMain()
{
A a = new A();
a.MyVar = 0;
//When passing by "reference", the method interacts directly with the caller's arguments.
Pass(ref a);
}
public void Pass(ref A x)
{
A b = new A();
b.MyVar = 1;
//This will change 'a' pointer
x = b;
// now 'a.MyVar' = 1
//In this case, assigning null to x also makes 'a' null, because this time were
//dealing with the original reference variable and not a copy of it.
x = null;
}
}
// 1> The reference portion of reference-typed local variables and parameters
// 2> Value-typed local variables and method parameters
//The following data is stored on the heap:
// 1> The content of reference-type objects.
// 2> Anything structured inside a reference-type object.
public class A
{
private int myVar;
public int MyVar
{
get { return myVar; }
set { myVar = value; }
}
}
public class Test1
{
public void TestMain()
{
//we create a local variable that references an object.
//The local variable is stored on the stack,
//while the object itself is stored on the heap
A a = new A();
a.MyVar = 0;
//In C#, parameters are (by default) passed by value,
//meaning that they are implicitly copied when passed to the method
Pass(a);
}
public void Pass(A x)
{
//at this line x and 'a' still point to the same object
//So this will be change on 'a.MyVar' to 9
x.MyVar = 9;
//Even this No effect on 'a' since x is a copy
x = null;
A b = new A();
x = b;
//at this line x and 'a' point to different object
//So this will be NO change on 'a.MyVar'
x.MyVar = 1;
}
}
public class Test2
{
public void TestMain()
{
A a = new A();
a.MyVar = 0;
//When passing by "reference", the method interacts directly with the caller's arguments.
Pass(ref a);
}
public void Pass(ref A x)
{
A b = new A();
b.MyVar = 1;
//This will change 'a' pointer
x = b;
// now 'a.MyVar' = 1
//In this case, assigning null to x also makes 'a' null, because this time were
//dealing with the original reference variable and not a copy of it.
x = null;
}
}
2 comments:
Oooo...kini ku tahu... :D
Mulai mengarah ke .NET nih blog-nya, keep in right track. Utk tahu grafik visitor-mu, blog-mu dikasih google analytics :D like my blog (http://firebird-with-delphi.blogspot.com)
WIH canggih
Mantabs
Woww keren
baru ku tahu
aku gak gaptek
Suit suit
Post a Comment