Returning Multiple Values Using Tuples In C#.

In this blog, I will show you how to return multiple values from a Tuple in C#, the code examples will return the values from a method and will return a string value, bool value and a list of strings. 

C# tuples are types that you define and have a lightweight syntax. Tuples can be named and unnamed; the advantage of named tuples is that you can specify the name as part of the tuple initialisation.

Once initialisation is done, and you add names to the values Visual Studio will help when you enter the '.' allowing you to pick the value you require easily.Returning Multiple Values Using Tuples In C#

This example has a helper class, a model class, MVC controller and the view. In the view, I will show the string, loop over the list in a foreach loop and display the value from a bool.

Helper Class

using System.Collections.Generic;
namespace Web.Helper.TuplesHelper
{
    public class MyDogList
    {
        public IEnumerable DogList()
        {
            List dogs = new List { "German Shepherd", "Siberian Husky", "Bernese Mountain Dog" };
            return dogs;
        }
        public string TestString    = "My favourite dogs is a : ";
        public bool ItsTrue         = true;
    }
}

Model Class

using System.Collections.Generic;
namespace Web.Model.TuplesViewModel
{
    public class TuplesViewModel
    {
        public string TestString                    { get; set; }
        public IEnumerable ListOfDogs       { get; set; }
        public bool IsItTrue                        { get; set; }
    }
}

Controller Class

using System.Linq;
using System.Web.Mvc;
using Web.Helper.TuplesHelper;
using Web.Model.TuplesViewModel;
namespace Web.UI.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var tupleValue = new MyDogList();
            var model = new TuplesViewModel
            {
                TestString  = tupleValue.TestString,
                ListOfDogs  = tupleValue.DogList().Where(x=>x.Contains("German Shepherd")),
                IsItTrue    = tupleValue.ItsTrue
            };
            return View(model);
        }
    }
}
Returning Multiple Values Using Tuples In C# Example

As you can see in the code examples above, I have an IEnumerable string that contains my list of dogs, I also have a test string and bool each returning values.

In the controller, you will see I have created a new view model and I'm populating the values of the model with my tuple values and also filtering the list to display only 'German Shepherd' before passing the view model to the view.

The code for the view is below:

View Code

@model Web.Model.TuplesViewModel.TuplesViewModel
@{
    ViewBag.Title = "Home Page";
}
<p>@Model.TestString</p>
<ul>
    @foreach (var data in Model.ListOfDogs)
    {
        <li>@data</li>
    }
</ul>
<p>It is @Model.IsItTrue</p

If we now run the code, you can see that the homepage now shows the return values that we have entered, this example uses hard-coded values, but we could have just as quickly returned the data from a database or XML file.

Returning Multiple Values Using Tuples In C#
Summary

Thank you for reading this blog, I hope you found it helpful