iTayb iTayb - 1 year ago 100
C# Question

Is it possible for a function to return two values?

Is it possible for a function to return two values?
Array is possible if the two values are both the same type, but how do you return two different type values?

Answer Source

Can a function return 2 separate values? No, a function in C# can only return a single value.

It is possible though to use other concepts to return 2 values. The first that comes to mind is using a wrapping type such as a Tuple<T1,T2>.

Tuple<int,string> GetValues() {
  return Tuple.Create(42,"foo");

The Tuple<T1,T2> type is only available in 4.0 and higher. If you are using an earlier version of the framework you can either create your own type or use KeyValuePair<TKey,TValue>.

KeyValuePair<int,string> GetValues() {
  return new KeyValuePair<int,sting>(42,"foo");

Another method is to use an out parameter (I would highly recomend the tuple approach though).

int GetValues(out string param1) {
  param1 = "foo";
  return 42;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download