C# : how to return more then one result.

4/1/2014·2 min read

When make some function, you might need to return more than one result. There're few ways you can do that using C#.

  • Return class. This works for different kind of type. For example, you can return int, string
  • Return list of same type. This works if you return same type. For example, List.
  • Return Tuple object.This also could return different kind of type.

1. Return class

definde a class. For example class Customer.

C# -- define a classpublic class Customer
public int Id { get ; set ;} 
public string Name { get ; set ;}

set value to the class, get the data.

C# -- get value from classCustomer customer = GetData();

2. Return list of same type data.

C# -- get value from ListList <string> list = GetData();

3. Use Tuple object to result different type of List values.

C# -- create method return Tuple class resultpublic Tuple < string , int , DateTime > GetData()
return new Tuple < string , int , DateTime >( "string" , 1, new DateTime(2011, 1, 1));
C# -- get value from Tuple class resultTuple < string , int , DateTime > list = GetData(); 
string str = list.Item1;
int innt = list.Item2;
DateTime dt = list.Item3;


I think Tuple is nice class, sometime, you want to return multi type of value by calling one method, but you don't want to create a new entity class to get that result. For this case, Tuple is good.

However, if you want to passing that result to use at many place, you should ceate a entity class. So, one in using in single method == Tuple. use as shared class == create entity class or using Generic List, etc.