
- 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
How to easily initialize a list of Tuples in C#?
Tuple can be used where you want to have a data structure to hold an object with properties, but you don't want to create a separate type for it. The Tuple<T> class was introduced in .NET Framework 4.0. A tuple is a data structure that contains a sequence of elements of different data types.
Tuple<int, string, string> person = new Tuple <int, string, string>(1, "Test", "Test1");
A tuple can only include a maximum of eight elements. It gives a compiler error when you try to include more than eight elements.
Tuples of List
var tupleList = new List<(int, string)> { (1, "cow1"), (5, "chickens1"), (1, "airplane1") };
Tuples of Array
var tupleArray = new(int, string)[] { (1, "cow1"), (5, "chickens1"), (1, "airplane1") };
Nested Tuples
var numbers = Tuple.Create(1, 2, 3, 4, 5, 6, 7, Tuple.Create(8, 9, 10, 11, 12, 13)); Tuple as a Method Parameter static void DisplayTuple(Tuple<int,string,string> person) { }
Tuple as a Return Type
static Tuple<int, string, string> GetTest() { return Tuple.Create(1, "Test1", "Test2"); }
- Related Articles
- Initialize tuples with parameters in Python
- How to initialize List in Kotlin?
- How to initialize a list to an empty list in C#?
- How to declare and initialize a list in C#?
- Convert list of tuples to list of list in Python
- Combining tuples in list of tuples in Python
- Python program to find Tuples with positive elements in a List of tuples
- Java Program to Initialize a List
- How to create a pandas DataFrame using a list of tuples?
- Count tuples occurrence in list of tuples in Python
- Convert list of strings to list of tuples in Python
- Convert list of tuples to list of strings in Python
- Remove duplicate tuples from list of tuples in Python
- How to initialize an empty array list in Kotlin?
- Python program to find Tuples with positive elements in List of tuples

Advertisements