Passing Data Back from a Method Using out Parameters

If you want a method to return a single value, you can return that value as the result of the method.
1
2
3
4
public int AgeInHumanYears()
{
    return Age * 7;
}
However, there might be times when you want to return more than one data item from a method.  You can do this without parameters.  Putting the keyword out in front of a parameter tells the compiler that the caller will not pass data in, but the method will pass data out.
1
2
3
4
5
6
public void GetDogVitals(out string fullName, out int age, out string barkPhrase)
{
    fullName = string.Format("{0}, {1}", Name, Title);
    age = Age;
    barkPhrase = BarkPhrase;
}
When you call the method, you also need to use the out keyword on the variables passed into the method.
1
2
3
4
string hisFullName;
int hisAge;
string hisBark;
kirby.GetDogVitals(out hisFullName, out hisAge, out hisBark);