C# Tuples

C# Code Snippets Tuples
Share:

About

In this code snippet, we will take a look at tuples in C#.

Tuples allow you to quickly and easily create objects. You can for example use them as a variable or method return data type.  

Let’s have a look at the code below to see how to use tuples.

Code:

(string myString, int myInt) myTuple = ("Hello World", 5);
Console.WriteLine(myTuple.myString + " " + myTuple.myInt);

var myOtherTuple = myMethod();
Console.WriteLine(myOtherTuple.Item1 + " " + myOtherTuple.Item2);

Console.ReadLine();

//Tuples can also be used as return types for methods.
static (int, int) myMethod() 
{
    return (5, 10);
}

Resulting output:

Share:

Leave a Reply

Your email address will not be published. Required fields are marked *

The following GDPR rules must be read and accepted:
This form collects your name, email and content so that we can keep track of the comments placed on the website. For more info check our privacy policy where you will get more info on where, how and why we store your data.

Advertisment ad adsense adlogger