//C# CODE//
how can i return more than 1 value?
//C# CODE//
how can i return more than 1 value?
Could return custom class/struct or array…
In what context?
But what mgear said.
Additionally, there’s also the out parameter.
i would like to return 3 values that i have in my function
i was trying to do
return value1 & value2 & value3;
but that doesn’t worked aswell
how can i return in an array?
Can you post the code you’re thinking of using?
You can usually use .ToArray() or an IEnumerator to return multiple values.
You can also specify array in return type like this:
int[] Foo(int a, int b, int c){
return new int[3] = {a, b, c};
}
Unfortunately, C# doesn’t support the concept of multiple return values per se. However, there are several ways of passing extra information out of a function that might help you, depending on your situation.
Option #1: Return one complicated variable that contains all your data
Instead of returning a simple type like “int” or “bool”, you could return a more complicated type such as an array, List, Dictionary, or even a custom struct or class that contains whatever data you need.
public struct myComplicatedReturnType {
int myInt;
bool myBool;
string myString;
}
myComplicatedReturnType myFunction() {
// Some code that creates and returns the appropriate struct
}
Option #2: Out (or Ref) Parameters
When you declare the arguments for your function, you can put the “ref” or “out” keyword in front of them to modify the way they’re passed. A “ref” parameter is passed by reference, so if the function modifies the value of that variable, the variable is also changed for the caller. An “out” parameter is like a “ref” parameter except that it doesn’t even have to be initialized before calling the function (you’re saying that the function doesn’t care about its initial value; it exists solely to pass information back out of the function).
bool myFunc(out int number) {
number = 5;
return true;
}
void otherFunc() {
int x;
bool b = myFunc(x);
// x now has the value 5 (and b has the value true)
}
Option #3: Pass handles as parameters, then modify the objects they reference
One drawback of ref/out parameters is that they can’t be optional; if they’re in the function definition, the caller must provide a variable for each of those parameters, even if they don’t care about the result.
But if you pass a class as just a normal “in” parameter to a function, you’re really passing a handle to the class, not a copy of the entire instance. So any modifications to that object made inside the function will show up outside it.
using System.Collections.Generic;
void myFunc(List<int> data = null)
{
if (data != null) {
data.Add(5);
data.Add(17);
data.Add(-2);
}
}
void otherFunc()
{
List<int> myList = new List<int>();
myFunc(myList);
// myList now contains 5, 17, and -2
}
Option #4: IEnumerable
This isn’t really a “general” solution to the problem of multiple return values, but there is a built-in language feature to return multiple values in one special case, which is when you’re dealing with iterators. You can return values “one at a time” using yield return, and then the caller can iterate through them with a foreach loop or manipulate the collection in a bunch of interesting ways using Linq.
using System.Collections.Generic;
using System.Linq;
IEnumerable<int> myFunc()
{
for (int i = 0; i < 10; ++i) {
yield return i*5;
}
}
void otherFunc()
{
foreach (int x in myFunc()) {
Debug.Log(x); // Logs 0, 5, 10, 15, etc.
}
int[] myArray = myFunc().ToArray(); // Creates an array containing 0, 5, 10, etc.
}