How to get a specific value from a method with multiple return values?

I want to access all of the int values when I call a method with the return line shown in the image, how would I go about writing it? e.g. I want to assign exp & gold to variables but don't know how to get them (is it done through an index???)
No description
4 Replies
Sehra
Sehra5d ago
var res = Method();
// res.exp or res.Item1
// res.gold or res.Item2
// res.bones or res.Item3
// res.parts or res.Item4
var (exp, gold, bones, parts) = Method();
var res = Method();
// res.exp or res.Item1
// res.gold or res.Item2
// res.bones or res.Item3
// res.parts or res.Item4
var (exp, gold, bones, parts) = Method();
two ways, either get the tuple, or deconstruct it to variables
this_is_pain
this_is_pain5d ago
you either give them a name (like return (Name: value, Name: value, ...); but names should be in the return type) so that you can access properties with that name or you have to use Item1, Item2, and so on
Sehra
Sehra5d ago
you can also use _to discard some if you don't need them
var (exp, _, bones, _) = Method();
var (exp, _, bones, _) = Method();
you need to name the parts in the return for first to work
public (int res, int gold, int bones, int parts) Method()
{
...
}
public (int res, int gold, int bones, int parts) Method()
{
...
}
Crazy Cactuar
Crazy CactuarOP5d ago
I was about to say that the first method didn't work so I used Item1-4 instead, but I named the parts in the method and it works if I do that, thanks

Did you find this page helpful?