I have very simple problem. I made a very simple function for you to demonstrate my problem.
static void Main(string[] args) { double[,] mydouble = new double[1, 4]; mynewMatrix(out mydouble); } public static void mynewMatrix(out double[,] d) { for (int i = 0; i < 4; i++) d[0, i] = i; }
Error:
Use of unassigned out parameter 'newMAt' The out parameter 'newMAt' must be assigned to before control leaves the current method
I don't know where is problem.
The out parameter in C# is used to pass arguments to methods by reference. It differs from the ref keyword in that it does not require parameter variables to be initialized before they are passed to a method. The out keyword must be explicitly declared in the method's definition as well as in the calling method.
ref is used to state that the parameter passed may be modified by the method. in is used to state that the parameter passed cannot be modified by the method. out is used to state that the parameter passed must be modified by the method.
String assignment is performed using the = operator and copies the actual bytes of the string from the source operand up to and including the null byte to the variable on the left-hand side, which must be of type string. You can create a new variable of type string by assigning it an expression of type string.
TLDR: Initialize a variable to null to indicate to the compiler that you plan to assign it later. Suppose you have a situation where you need to create a variable but you will be assigning a value to it within a conditional statement (if, for, foreach, while, etc).
If the array is defined OUTSIDE of the function, you should use a ref
(or nothing, considering the array is a reference type). out
means the parameter will be initialized in the function before it returns. Some examples of use:
static void Main(string[] args) { double[,] mydouble; mynewMatrix(out mydouble);// call of method double[,] mydouble2 = new double[1, 4]; mynewMatrix2(mydouble2);// call of method // useless for what you want to do mynewMatrix3(ref mydouble2);// call of method } public static void mynewMatrix(out double[,] d) { d = new double[1, 4]; for (int i = 0; i < 4; i++) { d[0, i] = i; } } public static void mynewMatrix2(double[,] d) { for (int i = 0; i < 4; i++) { d[0, i] = i; } } // useless for what you want to do public static void mynewMatrix3(ref double[,] d) { for (int i = 0; i < 4; i++) { d[0, i] = i; } }
I'll add that if you don't know what is the difference between ref
and out
you could read Difference between ref and out parameters in .NET
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:
void mynewMatrix(ref double[,] d)
void mynewMatrix(out double[,] d)
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With