A method can only return one value, but sometimes you need to output two pieces of information. Calling a method that uses an out
parameter is one way to return multiple values.
For example, the Int32.TryParse()
method tries to parse its input as an integer. If it can properly parse the input, the method returns true
and sets its out
variable to the new value. If it cannot properly parse the input, the method returns false
and sets the out
variable to 0.
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 a variable that has been declared of type int
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 0 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);
Instructions
Let’s parse another string ageAsString
to an integer.
First, define:
- an
int
namedageAsInt
- a
bool
namedoutcome
Use Int32.TryParse()
to convert ageAsString
:
ageAsInt
should be used as theout
argumentoutcome
should capture the returned value
Print outcome
and ageAsInt
to the console.
Repeat the process with nameAsString
:
Define:
- an
int
namednameAsInt
- a
bool
namedoutcome2
Use Int32.TryParse()
to convert nameAsString
:
nameAsInt
should be used as theout
argumentoutcome2
should capture the returned value
Print the returned value and the out
variable to the console.