A Tuple is a lightweight data structure in C# that can hold a fixed-size collection of elements, where each element can have a different type. The tuple allows you to group multiple values into a single object.
Tuples are often used for returning multiple values from a method or when you need to group data without creating a custom class or struct.
There are two way to create a tuple
- Using the Tuple class
- Using Tuple literals
Creating a Tuple using Tuple class
using System;
public class MyProgram
{
public static void Main()
{
// Creating a tuple with two elements using Tuple.Create
Tuple empTuple = Tuple.Create(1, "John");
// Accessing tuple elements using id, Name, etc.
Console.WriteLine($"ID: {empTuple.Item1}, Name: {empTuple.Item2}");
}
}
Output:
Note:
- Tuple<int, string>: This is a tuple that contains an int and a string.
- You access the elements in the tuple using Item1, Item2, and so on. The items are zero-indexed but are named Item1, Item2, etc., to indicate their order in the tuple.
Creating a Tuple using tuple literals (C# 7.0 and above)
using System;
public class MyProgram
{
public static void Main()
{
// Creating a tuple using tuple literal (C# 7.0 and above)
var empTuple = (1, "John");
// Accessing tuple elements using id, Name, etc.
Console.WriteLine($"ID: {empTuple.Item1}, Name: {empTuple.Item2}");
}
}
Output:
Explanation:
- var tuple = (1, “John”): This creates a tuple with an int and a string without explicitly specifying the types. The compiler will infer the types.
- The items are still accessible using Item1, Item2, etc
Named Tuples
Starting with C# 7.0, tuples can be named, which makes the code more readable. Instead of accessing items by Item1, Item2, etc., you can give the elements meaningful names.
using System;
public class MyProgram
{
public static void Main()
{
// Creating a named tuple
var employee = (Name: "John", Age: 35);
// Accessing tuple elements by name
Console.WriteLine($"Name: {employee.Name}, Age: {employee.Age}");
}
}
Output:
Explanation:
- (Name: “John”, Age: 35): This is a tuple where the elements are named Name and Age. You can access the elements directly using these names.
- This provides better clarity than using Item1, Item2, etc.
Advantages of Tuples
1. Easy Return of Multiple Values: Tuples are useful when you need to return multiple values from a method without creating a custom class or struct.
2. Improved Readability with Named Tuples: Named tuples make your code more understandable.
Java Tutorial – Interview Questions
Q 1: What is Tuple?
Ans: A data structure to store multiple values.
Q 2: Can Tuple store different data types?
Ans: Yes.
Q 3: Is Tuple immutable?
Ans: Yes.
Q 4: Max items in Tuple?
Ans: 8 (including nested tuple).
Q 5: Which namespace contains Tuple?
Ans: System.
C# Tuple – Objective Questions (MCQs)
Q1. What is the correct way to create a tuple in C#?
Q2. How can you access the second element of a tuple t created using Tuple.Create()?
Q3. How many elements can a C# tuple contain?
Q4. Which namespace contains the Tuple class?
Q5. Tuples are __________.