-1

I'm writing little code and my problem is that I'd like to do something like(I'm not C# master ^^):

private void pierwiastek(double* c, double* d)
{

}

But it seems to not work because unsafe and I don't want to use all that unsafe/fixed stuff, can this effect be reach in other way(I mean the variable won't be copied and function will work on source vars)?

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Makciek
  • 179
  • 1
  • 2
  • 9

2 Answers2

4

You are presumably looking for ref or out parameters:

private void square(double input, out double output)
{
    output = input * input;
}

For ref or out parameters, modifications made to the argument are made to the variable that the caller supplied. This is pass-by-reference rather than the default pass-by-value.

Call the function like this:

double d;
square(3.0, out d);
Debug.Assert(d == 9.0);

With an out parameter the information flows from the function to the caller. A ref parameter allows data to be passed both into the function as well as out of it.

These language features are documented on MSDN: ref and out. And no doubt your text book will cover this subject.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
1

You want to use either ref or out parameter.

private void pierwiastek(ref double c, ref double d)
{
   //some code
}

or

private void pierwiastek(out double c, out double d)
{
   //some code
}

You have to call the methode with either ref or out keyword:

pierwiastek(ref x, ref y);

or

pierwiastek(out x, out y);

The main difference between these two keywords is that variables that get passed with ref need to be initialized while out doesn't require this. You may want to read the corresponding msdn articles for further information:

ref - http://msdn.microsoft.com/de-de/library/14akc2c7.aspx

out - http://msdn.microsoft.com/de-de/library/t3c3bfhx.aspx