A method can only return one value, but sometimes you need to output two pieces of information.

For example the Int32.TryParse() method tries to parse its input as an integer. If it can, it returns true and sets a specific variable to the new value. If it cannot it returns false and sets a specific variable to null. This is what the method’s signature looks like:

public static bool TryParse (string s, out int result);

The method returns a boolean and accepts a string and int variable as input.

Here’s how Int32.TryParse() and the out parameter are used:

int number; bool success = Int32.TryParse("10602", out number); // number is 10602 and success is true int number2; bool success2 = Int32.TryParse(" !!! ", out number2); // number2 is null and success2 is false

The second parameter is labeled out, which means that it must be assigned a value within the method.

For a shortcut, you can declare the int variable within the method call:

bool success = Int32.TryParse("10602", out int number);



Let’s parse another string ageAsString to an integer.

First, define:

  • an int named ageAsInt
  • a bool named outcome

First, use Int32.TryParse() to convert ageAsString:

  • ageAsInt should be used as the out argument
  • outcome should capture the returned value

Print outcome and ageAsInt to the console.


Repeat the process with nameAsString:


  • an int named nameAsInt
  • a bool named outcome2

Use Int32.TryParse() to convert nameAsString:

  • nameAsInt should be used as the out argument
  • outcome2 should capture the returned value

Print the returned value and the out variable to the console.

Take this course for free

By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.
Already have an account?