Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of unassigned out parameter, c#

Tags:

c#

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.

like image 568
Shahgee Avatar asked Oct 11 '11 10:10

Shahgee


People also ask

What is the use of out parameter?

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.

What is the use of ref and out parameter in C#?

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.

What is the value of unassigned string?

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.

What is use of unassigned local variable in C#?

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).


2 Answers

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

like image 133
xanatos Avatar answered Sep 29 '22 04:09

xanatos


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:

  1. Assign the variable each time you call the method and use void mynewMatrix(ref double[,] d)
  2. Assign the variable once, inside your method and use void mynewMatrix(out double[,] d)
  3. 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?

like image 44
Buh Buh Avatar answered Sep 29 '22 04:09

Buh Buh