简体   繁体   中英

Better approach when obtaining multiple values from the same method at once

I have the following method which returns a tuple,

public Tuple<int, bool> GetStudentInformation(long stutID)

Called as,

Marks= GetStudentInformation((Id).Item1;
HasPassed= GetStudentInformation((Id).Item2;

This works fine as it is but I dont like that I call the same method twice to get item1 and item2, using Tuple is probably not the way ahead but any advice if c# has support to get two values returned over a single execution of the method?

You just need to save return value

Tuple<int, bool> info = GetStudentInformation(Id);

Marks = info.Item1;
HasPassed = info.Item2;

example of a clearer way I prefer when using tuples:

public (int data1, bool data2) GetStudentInformation(Id) {
    return (123, true);
}

var studentInfo = GetStudentInformation(111);
Console.Write(studentInfo.data1);
Console.Write(studentInfo.data2);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM