How to return more than one value from a method?

  • A+
Category:Languages

I want to return age values from a method in C# like the following

private int UpdateAges( int age1,  int age2,  int age3,  int age4) {     age1++;     age2++;     age3++;     age4++;     return (age1, age2, age3, age4); } 

and I can use it like the following

MessageBox.Show(UpdateAges(45, 30, 45, 30).ToString()); 

 


Try this:

private Tuple<int, int, int, int> UpdateAges( int age1,  int age2,  int age3,  int age4) {     age1++;     age2++;     age3++;     age4++;      return Tuple.Create(age1, age2, age3, age4); } 

See also: about C# 7 new included Tuples syntax

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: