In this article, we will discuss how to get the First Element of the ValueTuple in C#. But before going to its implementation, we must know about the tuple and valuetuple.
What is a Tuple?
A tuple denotes a data structure made up of multiple parts. A tuple is a data structure that provides the simplest method to describe a data collection with numerous values that may or may not be connected. It originated in .NET Framework 4.0. A tuple can include members ranging from 1 to 8. If we try to add more than eight items, the compiler will return an error. When we wish to construct a data structure that includes objects with their attributes, and we don't want to create a separate type for it, we often use tuples.
Features of Tuples:
There are several features of tuples. Some main features of the tuples are as follows:
- It enables us to combine numerous datasets into a single data collection.
- It enables us to produce, manipulate, and access datasets.
- It returns many values from a method without requiring the out argument.
- It can also save duplicate items.
- It allows us to send numerous values to a method using a single argument.
What is the Value Tuple?
The ValueTuple structure, introduced in C# 7.0, contains the value type Tuple. It is already included in the .NET Framework 4.7 and later versions. It allows us to save a data collection with various values that may or may not be connected. It is capable of storing components ranging from 0 to 8 and of various sorts. We may also store duplicate items in a value tuple.
The Item1 property is used to retrieve the first unidentified element of a given value tuple. It is applicable to all value tuples, including 1-ValueTuple, 2-ValueTuple, etc. Unlike Tuple, ValueTuples enables an easy method for creating and initializing ValueTuples.
Syntax:
It has the following syntax:
public T1 Itemvalue1;
Here, T1 represents the field value of a ValueTuple<> structure. This ValueTuple<> can be a 1-, 2-, 3-, 4-, 5-, 6-, 7-, or 8-valued tuple.
Example:
Let us take an example to illustrate the valuetuple in C#.
Filename: ValueTuple.cs
//Program to implement how to get the first element of the value tuple
using System;
class Valutuple{
// Main Method
static public void Main()
{
// A value tuple with one element
var ValTupl1 = ValueTuple.Create("The Variables of the ValueTuple");
Console.WriteLine("The C# Topics: ");
//The Item property is used to access
//the initial element of a 1-ValueTuple.
Console.WriteLine(ValTupl1.Item1);
//The value tuple consists of two elements
var ValTupl2 = ValueTuple.Create("Arrays",
"String Values");
// Accessing the first component
//of a 2-ValueTuple with the Item property.
Console.WriteLine(ValTupl2.Item1);
//The value tuple consists of three elements
var ValTupl3 = ValueTuple.Create("ArrayLists",
"Lists", "Queue value");
// Accessing the first component
//of a 3-ValueTuple with the Item property.
Console.WriteLine(ValTupl3.Item1);
//The value tuple consists of four elements
var ValTupl4 = ValueTuple.Create("Polymorphism", "Dictionary",
"LinkedList", "Interfaces");
// Accessing the first component
//of a 4-ValueTuple with the Item property.
Console.WriteLine(ValTupl4.Item1);
//The value tuple consists of five elements
var ValTupl5 = ValueTuple.Create("Identifiers", "Data Types values",
"Keywords", "The Access Modifiers", "Operators");
// Accessing the first component
//of a 5-ValueTuple with the Item property.
Console.WriteLine(ValTupl5.Item1);
//The value tuple consists of six elements
var ValTupl6 = ValueTuple.Create("Nullable Types", "Class",
"Structure", "Indexers", "Switch Statement", "Loops");
// Accessing the first component
//of a 6-ValueTuple with the Item property.
Console.WriteLine(ValTupl6.Item1);
//The value tuple consists of seven elements
var ValTupl7 = ValueTuple.Create("Inheritance", "The
Constructors","Encapsulation", "Polymorphism", "Static Class",
"Partial Classes", "this keyword");
// Accessing the first component
//of a 7-ValueTuple with the Item property.
Console.WriteLine(ValTupl7.Item1);
//The value tuple consists of seven elements
var ValTupl8 = ValueTuple.Create("Parameters", "Method Hiding",
"Optional Parameters", "Anonymous Method", "Partial Methods", "Local Functions", "Delegates", "Destructors");
// Accessing the first component
//of an 8-ValueTuple with the Item property.
Console.WriteLine(ValTupl8.Item1);
}
}
Output:
The C# Topics:
The Variables of the ValueTuple
Arrays
ArrayLists
Polymorphism
Identifiers
Nullable Types
Inheritance
Parameters
Explanation:
The program defines numerous ValueTuples , each with a distinct amount of components.
- var ValTupl1 = ValueTuple.Create("The Variables of the ValueTuple");: The Item1 property is used to retrieve the first member of each ValueTuple.
- The ValueTuple creates many ValueTuples (ValTupl1, ValTupl2,..., ValTupl8). ValueTuple.Create method.
- Each ValueTuple has a varied amount of string components that represent distinct C# subjects. The structure of ValueTuple creation and element access is consistently replicated for each tuple. In this example, Console.WriteLine(ValTupl1.Item1) outputs the first component in every ValueTuple.