2

I have the following code.

using System;

class program
{
    static void Main()
    {
        string StrNumber = "100TG";
        int result = 0;

        bool IsConversionSuccessful = int.TryParse(StrNumber, out result);

        if (IsConversionSuccessful)
        {
            Console.WriteLine(result);
        }
        else
        {
            Console.WriteLine("Invalid");
        }
    }
}

I know that TryParse method tries to convert StrNumber(100TG) into an integer.

And if it succeeds, it's going to save converted value into a result variable and return true for boolean. And if it fails, a result value will remain as 0 and it will return false for boolean.

My question is that no matter what kind of boolean value IsConversionSuccessful variable gets, wouldn't "if(IsConversionSuccessful)" get activated? Am I misunderstanding TryParse Method?

jelly
  • 59
  • 2
  • 10
  • 1
    Yeah, you already admit the variable could be false, so isn't it obvious the `else` block is enterable? Or are you asking this because the use of a constant string value means `TryParse` will always return the same value in this code? – BradleyDotNET Sep 26 '15 at 00:32
  • Could you tell me as to why if(IsConversionSuccessful) won't run when IsConversionSuccessful is false? – jelly Sep 26 '15 at 01:05
  • @jelly The body of an `if` executes only when the condition evaluates to `true`. Look at the first paragraph of my answer. – Acha Bill Sep 26 '15 at 01:06
  • Are you interested in whether `if(IsConversionSuccessful)` gets executed or whether its' body gets executed? – Acha Bill Sep 26 '15 at 01:09
  • I didn't know that 'if' executes only when the condition is true. I thought it works on every condition. Thank you for your help Jafar :) – jelly Sep 26 '15 at 01:10
  • @jelly consider marking an answer. – Acha Bill Sep 26 '15 at 01:12
  • @Jafar I didn't see your previous question about if(IsConversionSuccessful) or its body getting executed. Could you explain it please? – jelly Sep 26 '15 at 01:21
  • The `if()` it self gets executed. The body following depends on the result of the condition – Acha Bill Sep 26 '15 at 01:30

2 Answers2

2

If IsConversionSuccessful gets to be false, then the condition if(IsConverstionSuccessful) evaluates to if(false). Hence, the body of that if does not execute.

The TryParse method does not determine the execution of the next line in your program. It simply tells you whether the conversion from string to int was successful or not by returning a boolean value.

The lines following TryParse is up to you.

Acha Bill
  • 1,255
  • 1
  • 7
  • 20
1

As you clearly have pointed out,

bool IsConversionSuccessful = int.TryParse(StrNumber, out result);

Will set IsConversionSuccessful to true/false based on how the method parsed the number.

If statements in themself evaluate something and always get a boolean answer, true or false. This is because if statements act like binary branch trees. They work like this: enter image description here

When you evaluate if(A), which in your case is

if (IsConversionSuccessful)

The processor decides on a path to take and execution after that depends on the decision the processor made. Note that even excluding the else branch, an empty else branch simply points back to the "..." that comes after the if statement.

Joachim Olsson
  • 316
  • 1
  • 8
  • Could you tell me as to why if(IsConversionSuccessful) won't run when IsConversionSuccessful is false? – jelly Sep 26 '15 at 01:05