Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a variable as an out argument at point of declaration

When reading a comment to an answer I saw the following construct to declare and initialize a variable:

int variable = int.TryParse(stringValue, out variable) ? variable : 0;

Is this allowed, correct and well defined in C#? What happens under the hood? Is the following what happens?

  1. Is variable first initialized to zero?
  2. then passed to int.TryParse (which assigns a value)?
  3. then optionally read (if int.TryParse return true)?
  4. and then, once again assigned/initialized?
like image 939
dalle Avatar asked Sep 10 '15 08:09

dalle


People also ask

Do you need to declare an out variable before use it?

Calling a method with an out argument In C# 6 and earlier, you must declare a variable in a separate statement before you pass it as an out argument. The following example declares a variable named number before it is passed to the Int32.

What is the best way of declaring an out parameter that is never going to be used?

While you can't actually make the out parameter optional, you could simply create an overload for the function without the out parameter, which would then take away the need to create a temporary variable. This answer is more functional. No need to modify later on if you want to use it or not use it anymore.

How can an out parameter be used in C#?

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.

Can you declare a variable in a parameter?

Parameter, local, and instance variablesParameters are declared in between the parentheses in the header of a method. Local variables are declared between the curly-braces of a method, in a statement (which needs to end with a semicolon).


2 Answers

Yes you are right for execution. You can also look into MSIL generated here

C# Code

 string stringValue = "5";
 int variable = int.TryParse(stringValue, out variable) ? variable : 0;

MSIL generated

1.  IL_0000:  nop    
2.  IL_0001:  ldstr      "5" // load string
3.  IL_0006:  stloc.0
4.  IL_0007:  ldloc.0
5.  IL_0008:  ldloca.s   variable
6.  IL_000a:  call       bool [mscorlib]System.Int32::TryParse(string, int32&)
7.  IL_000f:  brtrue.s   IL_0014
8.  IL_0011:  ldc.i4.0
9.  IL_0012:  br.s       IL_0015
10. IL_0014:  ldloc.1
11. IL_0015:  stloc.1
12. IL_0016:  ret

Which clarifies what it does behind the scene.

Statement 5 is allocating the variable onto stack. Statement 6 is calling the method. Statement 7,8,9 are actually exeuting the bool expression.

like image 153
D J Avatar answered Sep 22 '22 05:09

D J


This is a trick that happens to work because it is simply a rewriting of an ordinary if-statement. This code is equivalent to this:

int variable;
if (int.TryParse(stringVariable, out variable))
    variable = variable;
else
    variable = 0;

The sequence is as follows:

int.TryParse is called, variable is not initialized before this but it doesn't have to either. An out parameter does not require a definite assigned variable. As part of the method execution, the variable will be given a value, and int.TryParse will return true or false.

If the method returns true then the result of the expression will be variable and thus we will execute basically variable = variable.

If the method returns false then the result of the expression will instead be 0, and variable will now be given the value 0 regardless of what it was given as part of int.TryParse. In this case, however, this will not change the variable because int.TryParse has already given the variable a default value when it returns false which also happens to be 0.

This is basically a way to get everything onto one line.

Personally I would've written this code like this:

int variable;
int.TryParse(stringValue, out variable);
like image 28
Lasse V. Karlsen Avatar answered Sep 21 '22 05:09

Lasse V. Karlsen