How to return multiple values from C# method

In C#, you can return multiple values from a method using various approaches, such as tuples, out parameters, or custom classes. Here are examples of each approach:

1. Tuples (C# 7.0 and later)

Tuples are an easy and concise way to return multiple values. You can create a tuple by specifying the types and names of the elements.

public static (int, string) GetPerson()
{
    int id = 1;
    string name = "John";

    return (id, name);
}

// Use the method and destructure the tuple
(int id, string name) = GetPerson();
Console.WriteLine($"ID: {id}, Name: {name}");

2. Out parameters

You can use out parameters to return multiple values from a method. The method signature specifies the out parameters, and the caller must supply variables to receive the values.

public static void GetPerson(out int id, out string name)
{
    id = 1;
    name = "John";
}

// Use the method with out parameters
int id;
string name;
GetPerson(out id, out name);
Console.WriteLine($"ID: {id}, Name: {name}");

In C# 7.0 and later, you can use inline variable declaration for the out parameters:

GetPerson(out int id, out string name);

3. Custom class or struct

You can create a custom class or struct to hold the multiple values you want to return.

public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public static Person GetPerson()
{
    return new Person { Id = 1, Name = "John" };
}

// Use the method and access the properties
Person person = GetPerson();
Console.WriteLine($"ID: {person.Id}, Name: {person.Name}");

Choose the approach that best fits your needs. Tuples are often preferred when you need a simple, lightweight way to return multiple values, while custom classes or structs are useful when you want to create more complex or reusable data structures. out parameters are less commonly used due to their less intuitive syntax, but they can be useful in certain scenarios, such as when you need to return multiple values and have a method with a non-void return type.

  1. Returning multiple values from a method in C#

    In C#, you can use various techniques to return multiple values from a method. One common approach is to use Tuple or ValueTuple.

    public (int, string) GetMultipleValues()
    {
        return (42, "Hello");
    }
    
  2. Returning a tuple from a C# method

    public Tuple<int, string> GetTuple()
    {
        return Tuple.Create(42, "Hello");
    }
    
  3. C# out parameters for multiple return values

    public void GetMultipleValues(out int number, out string message)
    {
        number = 42;
        message = "Hello";
    }
    
  4. Returning a custom object with multiple properties in C#

    public class ResultObject
    {
        public int Number { get; set; }
        public string Message { get; set; }
    }
    
    public ResultObject GetResultObject()
    {
        return new ResultObject { Number = 42, Message = "Hello" };
    }
    
  5. C# method with ref parameters for multiple results

    public void GetMultipleValues(ref int number, ref string message)
    {
        number = 42;
        message = "Hello";
    }
    
  6. Returning a Dictionary or KeyValuePair from a method in C#

    public Dictionary<string, object> GetDictionary()
    {
        var result = new Dictionary<string, object>
        {
            { "Number", 42 },
            { "Message", "Hello" }
        };
        return result;
    }
    
  7. C# return multiple values using anonymous types

    public object GetAnonymousType()
    {
        return new { Number = 42, Message = "Hello" };
    }
    
  8. Returning a list or array from a method in C#

    public int[] GetArray()
    {
        return new int[] { 42, 43, 44 };
    }
    
  9. C# return multiple values with out parameters and tuples

    Combining out parameters with tuples:

    public void GetMultipleValues(out int number, out string message)
    {
        number = 42;
        message = "Hello";
    }
    
  10. C# method with multiple return statements

    public int GetNumber(bool condition)
    {
        if (condition)
            return 42;
        else
            return 0;
    }
    
  11. C# return multiple values using dynamic objects

    public dynamic GetDynamicObject()
    {
        return new { Number = 42, Message = "Hello" };
    }
    
  12. Returning multiple values using ref returns in C#

    public ref int GetNumberRef(int[] array, int index)
    {
        return ref array[index];
    }
    
  13. C# method returning multiple values using deconstruction

    public (int number, string message) GetMultipleValues()
    {
        return (42, "Hello");
    }
    
    // Usage:
    (int myNumber, string myMessage) = GetMultipleValues();