Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return multiple values in C# 7? [closed]

Tags:

c#

.net

c#-7.0

Is it is possible to return multiple values from a method natively?

like image 493
Coder Newbie Avatar asked Mar 21 '17 11:03

Coder Newbie


People also ask

How can I return multiple values from a function in C?

We can return more than one values from a function by using the method called “call by address”, or “call by reference”. In the invoker function, we will use two variables to store the results, and the function will take pointer type data. So we have to pass the address of the data.

Can you return multiple values?

Summary. JavaScript doesn't support functions that return multiple values. However, you can wrap multiple values into an array or an object and return the array or the object. Use destructuring assignment syntax to unpack values from the array, or properties from objects.

Can you return multiple types in C?

No, you can't return multiple types.

Can you return more than one value in C++?

A C++ function can return only one value. However, you can return multiple values by wrapping them in a class or struct. Or you could use std::tuple , if that is available with your compiler.


1 Answers

What do you mean by natively?

C# 7 has a new feature that lets you return more than one value from a method thanks to tuple types and tuple literals.

Take the following function for instance:

(string, string, string) MyCoolFunction() // tuple return type {        //...             return (firstValue, secondValue, thirdValue); } 

Which can be used like this:

var values = MyCoolFunction(); var firstValue = values.Item1; var secondValue = values.Item2; var thirdValue = values.Item3; 

Or by using deconstruction syntax

(string first, string second, string third) = MyCoolFunction();  //...  var (first, second, third) = MyCoolFunction(); //Implicitly Typed Variables 

Take some time to check out the Documentation, they have some very good examples (this answer's one are based on them!).

like image 183
Sid Avatar answered Sep 29 '22 01:09

Sid