In this guide, we will explore the process of accessing the initial element of a ValueTuple in C#. Prior to delving into the practical execution, it is essential to understand the concepts of tuple and ValueTuple.
What is a Tuple?
A tuple represents a composite data structure comprising multiple components. It serves as a straightforward approach to represent a group of data with various values that are not necessarily related. This concept was introduced in the .NET Framework 4.0. A tuple can accommodate elements from 1 to 8. If there is an attempt to exceed eight elements, the compiler will generate an error. Tuples are commonly employed when there is a need to combine objects along with their characteristics without defining a distinct type for them.
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 construct, unveiled in C# 7.0, encompasses the Tuple value type. It comes pre-installed in the .NET Framework 4.7 and subsequent iterations. This data structure enables the storage of a collection of data with disparate and potentially unrelated values. It has the ability to hold elements between 0 and 8 in number and of diverse types. Additionally, duplicative entries can be accommodated within a value tuple.
The Item1 attribute is employed to fetch the initial unclassified element within a specified value tuple. It is relevant to all value tuples, such as 1-ValueTuple, 2-ValueTuple, and so forth. In contrast to Tuple, ValueTuples provide a straightforward approach to generate and set up ValueTuples.
Syntax:
It has the following syntax:
public T1 Itemvalue1;
T1 denotes the value stored within a ValueTuple<> construct. The ValueTuple<> can encompass tuples with 1, 2, 3, 4, 5, 6, 7, or 8 values.
Example:
Let's consider an instance to demonstrate 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.