-
a strange thing
Hi;
I create a namespace and inside the class there is one function:
namespace mynamespace
{
public class TEST
{
public void test(int i)
{
i = 1;
}
}
}
made it as *.dll, and loaded it into another project:
.........
public class Mytest
{
public static void Main()
{
Mytest mytest = new Mytest();
mytest.test1();
}
public void test1()
{
int t=0;
mynamespace.TEST Test = new mynamespace.TEST();
Test.test(t);
Console.WriteLine(t);
}
so the output should be 1, wasn't it?
but it was 0, what was wrong?
this was weird :(
-
There is a basic difference in all programming languages by passing a variable by value ( as you did ) and by reference ( what you wanted to do ).
Passing it by value means your local variable i in the function is a copy of what you gave as parameter. So modifying i does not change the variable given as parameter. By reference means no variable is copied, only a reference to the original variable is given. Changing the reference changes the original variable as you want it in this case.
Look up the in, out and ref keywords in C# for a better understanding of passing by value or by reference.
-