

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- Initialize tuples with parameters in Python
- How to initialize a list to an empty list in C#?
- Combining tuples in list of tuples in Python
- Convert list of tuples to list of list in Python
- Java Program to Initialize a List
- How to declare and initialize a list in C#?
- Python program to find Tuples with positive elements in a List of tuples
- Count tuples occurrence in list of tuples in Python
- How to create a pandas DataFrame using a list of tuples?
- Remove duplicate tuples from 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
- Update a list of tuples using another list in Python
- Python program to sort a list of tuples alphabetically
- Convert list of tuples into list in Python
Advertisements