In c# there are two very similar keywords, ref
and out
.
Both of them pass values by reference, but the difference is:
When you use ref
the compiler will require you to assign your variable prior to calling the method.
When using out
it will not require this. This means that you will not be able to assume that the parameter has already been populated. You will not be able to read its value inside the method.
To illustrate the problem, just imagine what would happen if someone else wrote this code to call your method:
double[,] myUnassignedDouble;
mynewMatrix(out myUnassignedDouble);
Clearly the variable will never be assigned, which is bad.
This leaves you with three options:
- Assign the variable each time you call the method and use
void mynewMatrix(ref double[,] d)
- Assign the variable once, inside your method and use
void mynewMatrix(out double[,] d)
- Assign the variable each time you call the method and use
void mynewMatrix(double[,] d)
The third option will work because so far you don't seam to need to reassign your variable. Obviously that might change as your code becomes more complicated. I assume you did have your reasons for using out
in the first place?