Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C# Data Types
  • C# Decision Making
  • C# Methods
  • C# Delegates
  • C# Constructors
  • C# Arrays
  • C# ArrayList
  • C# String
  • C# Tuple
  • C# Indexers
  • C# Interface
  • C# Multithreading
  • C# Exception
Open In App
Next Article:
C# ValueTuple <T1,T2,T3,T4,T5,T6> Struct
Next article icon

C# ValueTuple<T1, T2> Struct

Last Updated : 25 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

ValueTuple<T1, T2> is a struct in C# provided by the System namespace. It is used to create a tuple that stores a pair value tuple or 2-ValueTuple. The ValueTuple<T1, T2> Struct is a value type which means it is stored on the stack and it is mutable. We can change its value after creation. Tuple class provides better performance and memory management.

Creating a ValueTuple<T1, T2> Instance

ValueTuple<T1, T2> does not have a publicly accessible constructor, so the preferred method of creating it is through the static Create() method, or by initializing the Item1 and Item2 fields directly.

Using the Create() Method:

// Creating a ValueTuple using Create() method

var vt = ValueTuple.Create(1, “Geek”);

Direct Initialization:

// Initializing a ValueTuple directly with Item1 and Item2

var vt = new ValueTuple<int, string> { Item1 = 1, Item2 = “Geek” };

Fields

ValueTuple<T1, T2> has the following fields:

  • Item1: It represents the first element of the tuple.
  • Item2: It represents the second element of the tuple

These fields can be accessed and modified directly.

Example: Demonstration of access to the ValueTuple<T1,T2> element in C#.

C#
// Accessing ValueTuple<T1,T2> elements using System;  class Geeks { 	static public void Main() 	{ 		// Creating a value tuple  		// Using Create method  		var vt = ValueTuple.Create(1, "Geek");  		// Display the element of the given value tuple  		Console.WriteLine("Details: "); 		Console.WriteLine("Id: {0}", vt.Item1); 		Console.WriteLine("Name: {0}", vt.Item2); 	} } 

Output
Details:  Id: 1 Name: Geek 

Explanation: In the above example, it creates two ValueTuple of different types 1 and Geek, prints the value of Item1 and item2 to the console.

Methods

Method

Description

CompareTo(ValueTuple)

It compares the current ValueTuple<T1> instance to a specified ValueTuple<T1> instance.

Equals(Object)

It returns a value that indicates whether the current ValueTuple<T1> instance equals a specified object.

Equals(ValueTuple<T1>)

It returns a value that indicates whether the current ValueTuple<T1> instance equals a specified ValueTuple<T1> instance.

GetHashCode()

It calculates the hash code for the current ValueTuple<T1> instance.

ToString()

It returns a string that represents the value of this ValueTuple<T1> instance.

Example: Checking if Two ValueTuples are Equal or not

C#
// Check if value tuples are Equal using System;  class Geeks { 	static public void Main() 	{ 		// Creating 2-ValueTuple  		// Using Create method  		var T1 = ValueTuple.Create(357,357); 		var T2 = ValueTuple.Create(93, 40);  		// Check if both the value tuples  		// are equal or not  		if (T1.Equals(T2)) 			Console.WriteLine("Tuple matched."); 		else 			Console.WriteLine("Tuple not matched."); 	} } 

Output
Tuple not matched. 

Key Features:

  • Two Element: A ValueTuple<T1, T2> contains two elements, referred to as Item1 and Item2
  • Mutable: The value of Item1 and Item2 can be changed after the tuple is created.
  • Type-Safe: The type of Item1 and Item2 are specified when the tuple is created, ensuring type safety.
  • Duplicates: It also allows to store duplicate values.
  • Implements Interfaces: ValueTuple<T1, T2> implements several interfaces such as:
    • IStructuralComparable
    • IStructuralEquatable
    • IComparable<ValueTuple<T1, T2>>
    • IEquatable<ValueTuple<T1, T2>>
    • ITuple: This makes it useful in various comparison and equality scenarios.


Next Article
C# ValueTuple <T1,T2,T3,T4,T5,T6> Struct
author
ankita_saini
Improve
Article Tags :
  • C#
  • CSharp-ValueTuple

Similar Reads

  • C# ValueTuple <T1> Struct
    ValueTuple<T1> struct in C# is part of the System namespace and it is used to create a tuple that stores a single component. It offers a more lightweight, value-type tuple compared to the older Tuple class and provides better performance and memory management. The ValueTuple<T1> struct i
    2 min read
  • C# ValueTuple<T1,T2,T3> Struct
    ValueTuple<T1, T2, T3> struct in C# is part of the System namespace and is used to store a tuple with three values. It is a value type and provides better performance and memory management compared to the traditional Tuple class. Also, the ValueTuple<T1, T2, T3> is mutable which means th
    3 min read
  • C# ValueTuple <T1,T2,T3,T4> Struct
    ValueTuple <T1, T2, T3, T4> struct in C# is part of the System namespace and it is used to create a tuple that stores quadruple or 4-ValueTuple. It offers a more lightweight, value-type tuple compared to the older Tuple class and provides better performance and memory management. The ValueTupl
    3 min read
  • C# ValueTuple <T1,T2,T3,T4,T5> Struct
    ValueTuple <T1, T2, T3, T4, T5> struct in C# is part of the System namespace and is used to create a tuple that stores quintuple or 5-ValueTuple. It offers a more lightweight, value-type tuple compared to the older Tuple class and provides better performance and memory management. The ValueTup
    3 min read
  • C# ValueTuple <T1,T2,T3,T4,T5,T6> Struct
    ValueTuple <T1, T2, T3, T4, T5, T6> struct in C# is part of the System namespace and is used to create a tuple that storessextuple or 6-ValueTuple. It offers a more lightweight, value-type tuple compared to the older Tuple class and provides better performance and memory management. The ValueT
    3 min read
  • C# ValueTuple <T1,T2,T3,T4,T5,T6,T7> Struct
    ValueTuple <T1, T2, T3, T4, T5, T6, T7> struct in C# is part of the System namespace and is used to create a tuple that stores septuple or 7-ValueTuple. It offers a more lightweight, value-type tuple compared to the older Tuple class and provides better performance and memory management. The V
    3 min read
  • C# ValueTuple Struct
    ValueTuple Struct in C# is a structure that provides static methods that are used in creating value tuples. It is defined under the System namespace and was introduced in .NET Framework 4.7. This struct enables runtime implementation tuples in C#. The ValueTuple structure represents a tuple that can
    4 min read
  • C# - ValueTuple <T1,T2,T3,T4,T5,T6,T7,TRest> Struct
    <ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> struct in C# is part of the System namespace and is used to create n-ValueTuple where n = 8 or greater than 8. It offers a more lightweight, value-type tuple compared to the older Tuple class and provides better performance and memory managemen
    4 min read
  • C# Tuple<T1,T2> Class
    The Tuple<T1, T2> is used to create a 2-tuple or pair. It represents a tuple which contains the two elements in it. We can instantiate a Tuple<T1, T2> object by calling either the Tuple<T1, T2>(T1, T2) constructor or the static Tuple.Create method. We can retrieve the value of the
    2 min read
  • Comparing two ValueTuple<T1> in C#
    ValueTuple is a structure introduced in C# 7.0 which represents the value type Tuple. It is already included in .NET Framework 4.7 or higher version. It allows you to store a data set that contains multiple values that may or may not be related to each other. You can also compare the instance of two
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences