I'm trying to convert C++ code into C#. We have functions that accept pointers in C++. In C# we are having troubles doing it. We have tried the following demo code:
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Test obj = new Test();
int a,b;
Console.WriteLine("Initial Values");
a = 20;
b = 100;
obj.SetRef(ref a,ref b);
Console.WriteLine("Value of A: " + a);
Console.WriteLine("Value of B: " + b);
obj.SetValRef(ref a);
Console.WriteLine("After Ref");
Console.WriteLine("Value of A: " + a);
Console.WriteLine("Value of B: " + b);
Console.ReadKey();
}
}
class Test
{
public void SetRef(ref int x, ref int y)
{
y = x;
}
public void SetValOut(out int x)
{
x = 10;
}
public void SetValRef(ref int x)
{
x = 10;
}
}
}
When we run it the output is
Initial Values
Value of A: 20
Value of B: 20
After Ref
Value of A: 10
Value of B: 20
We want that if value of one variable is changed then the value of second should automatically change( pointers).