
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
What are Tuples in C#4.0?
Tuples has a sequence of elements of different data types. It was introduced to return an instance of the Tuple<T> with no need to specify the type of each element separately.
Let us create a tuple with two elements. The following is how you declare a tuple. −
Tuple<int, string>person = new Tuple <int, string>(32, "Steve");
Now, for the example check for the first item in the tuple, which is an integer −
if (tuple.Item1 == 99) { Console.WriteLine(tuple.Item1); }
Now check for second item in the tuple, which is a string −
if (tuple.Item2 == "Steve") { Console.WriteLine(tuple.Item2); }
The following is an example to create a tuple with string and int items −
Example
using System; using System.Threading; namespace Demo { class Program { static void Main(string[] args) { Tuple<int, string> tuple = new Tuple<int, string>(50, "Tom"); if (tuple.Item1 == 50) { Console.WriteLine(tuple.Item1); } if (tuple.Item2 == "Jack") { Console.WriteLine(tuple.Item2); } } } }
Output
50
- Related Articles
- What are "named tuples" in Python?
- What are the differences and similarities between tuples and lists in Python?
- Combining tuples in list of tuples in Python
- Count tuples occurrence in list of tuples in Python
- What is a Pair class in Java Tuples?
- What is a Septet class in Java Tuples?
- What is a Quintet class in Java Tuples?
- What is a Triplet class in Java Tuples?
- How to Concatenate tuples to nested tuples in Python
- Remove duplicate tuples from list of tuples in Python
- Check if two list of tuples are identical in Python
- Updating Tuples in Python
- Nested Tuples in C#
- Compare tuples in Python
- How to split Python tuples into sub-tuples?

Advertisements