
- 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
When to use tuples in C#?
Tuples are used when you want to return multiple values from a method without using ref or out parameters.
Tuples are used to pass multiple values to a method through a single parameter. Tuples can also be nested together
Example
Passig Tuples as a Parameter
class Program{ static void DisplayTupleValues(Tuple<int, string, string> dummy){ Console.WriteLine($"Id = { dummy.Item1}"); Console.WriteLine($"Value1 = { dummy.Item2}"); Console.WriteLine($"Value2 = { dummy.Item3}"); } static void Main(){ var dummy = Tuple.Create(1, "Dummy", "Tuple"); DisplayTupleValues(dummy); Console.ReadLine(); } }
Output
Id = 1 Value1 = Dummy Value2 = Tuple
Example
Tuple as a return type
class Program{ static Tuple<int, string, string> ReturnTuple(){ return Tuple.Create(1, "Value1", "Value2"); } static void Main(){ var returnValues = ReturnTuple(); System.Console.WriteLine($"{returnValues.Item1} {returnValues.Item2} {returnValues.Item3}"); Console.ReadLine(); } }
Output
1 Value1 Value2
- Related Articles
- When to use inline function and when not to use it in C/C++?
- When to use an abstract class and when to use an interface in Java?
- How to Concatenate tuples to nested tuples in Python
- When to use virtual destructors in C++?
- When to use extern in C/C++
- When to use @JsonAutoDetect annotation in Java?
- When to use vararg methods in Java?
- When to use fillInStackTrace() method in Java?
- When to use $(document).ready() and when $(window).load() in jQuery?
- How to split Python tuples into sub-tuples?
- When should I use an Inline script and when to use external JavaScript file?
- Combining tuples in list of tuples in Python
- How to recognize when to use : or = in JavaScript?
- When to use i++ or ++i in C++?
- When to use LinkedList over ArrayList in Java

Advertisements