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.

Tuple values

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.


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.

Summary

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

Leave a comment

*
*