How to Returning Multiple Values from method Without Using Class, ref, or out Keyword ?
Lalji Dhameliya
Select an image from your device to upload
In C#, one approach to return multiple values without classes, ref, or out keywords is by utilizing tuples. Tuples allow combining multiple values into a single return value. Here’s an example demonstrating this technique:
using System;class Program{ static (int, string) GetMultipleValues() { int number = 10; string text = "Hello"; return (number, text); } static void Main() { var result = GetMultipleValues(); Console.WriteLine($"Number: {result.Item1}, Text: {result.Item2}"); }}
using System;
class Program
{
static (int, string) GetMultipleValues()
int number = 10;
string text = "Hello";
return (number, text);
}
static void Main()
var result = GetMultipleValues();
Console.WriteLine($"Number: {result.Item1}, Text: {result.Item2}");
The GetMultipleValues method returns a tuple containing an integer and a string. By deconstructing the tuple, you can access the individual values. Tuples provide a concise way to return multiple values without the need for additional classes or keywords.