Javatpoint Logo
Javatpoint Logo

ValueTuple Struct in C#

In the realm of C#, the evolution of language features and capabilities has been remarkable, and one such augmentation that significantly enriches the language's expressiveness is the ValueTuple struct. Emerged in C# 7.0, the ValueTuple struct offers a lightweight mechanism for amalgamating multiple values into a singular structure, presenting a convenient alternative to conventional classes or tuples. In this blog post, we will immerse ourselves in the syntax, code, and practical examples of utilizing the ValueTuple structure to gain a deeper understanding of its potential and advantages.

Before delving into practical illustrations, let's dissect the fundamental syntax of the ValueTuple struct. A ValueTuple can be declared either with or without named elements. Here's the basic syntax for both scenarios:

Without Named Elements:

With Named Elements:

In the scenario with named elements, each element is assigned a name using the colon (:) syntax. It can significantly enhance code readability and make it more lucid when accessing tuple values. Now, let's delve into code examples to elucidate the application of ValueTuple in diverse scenarios.

Example 1: Fundamental ValueTuple

Output:

Item1: 42, Item2: Hello, Item3: 3.14
Name: John, Age: 25, IsStudent: False

Explanation:

  • Tuple Initialization: The provided code initiates two tuples, each serving a distinct purpose. The first tuple, referred to as unnamedTuple, encapsulates an integer (42), a string ("Hello"), and a floating-point number (3.14). On the other hand, the second tuple, denoted as namedTuple, adopts a more structured approach by incorporating named elements such as "Name", "Age", and "IsStudent", each associated with specific values.
  • Element Retrieval: The code exemplifies the retrieval of elements from both tuples. For unnamedTuple, it employs the ItemX properties (e.g., Item1, Item2) to access and display the values using the WriteLine method. In contrast, the namedTuple takes advantage of named properties (e.g., Name, Age) for a more discernible approach. This strategic use of named elements enhances the clarity of code, especially in scenarios involving tuples with multiple values.
  • Enhanced Clarity: The code achieves improved code clarity by incorporating named elements in the second tuple. This practice facilitates a more straightforward comprehension of the intended purpose of each element, particularly in instances where tuples encompass diverse values.
  • Output Display: The final console output serves as a visual representation, showcasing the values of elements from both tuples. This demonstration elucidates the process of extracting and presenting individual elements from tuples, irrespective of whether they employ named or unnamed elements.

Example 2: Returning Multiple Values from a Method

Output:

Min: 1, Max: 9

Explanation:

  • Method Objective: The primary goal of the GetMinMax method is to determine the minimum and maximum values within a given array of integers.
  • Input Validation: A crucial aspect of the method involves checking for the validity of the input array. It throws an ArgumentException if the array is either null or empty.
  • Variable Initialization: The method initializes two variables, namely min and max, starting with the first element of the provided integer array.
  • Iterative Process: Through iteration over the array, the method continuously updates the min and max variables based on comparisons with each element.
  • Tuple Result: The method concludes by returning a tuple containing the computed minimum and maximum values.
  • Main Method Execution: Within the Main method, an integer array is defined, and the GetMinMax method is called for execution.
  • Deconstruction Operation: The tuple result is deconstructed into individual min and max variables to enhance readability.
  • Output Display: The console effectively presents the minimum and maximum values on the WriteLine.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA




news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news