一次从同一方法获取多个值时的更好方法

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

我有以下方法returns一个元组,

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

被称为,

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

这很好用,但我不喜欢我两次调用相同的方法来获取 item1 和 item2,使用 Tuple 可能不是前进的方向,但如果 c# 支持通过方法的单次执行?

您只需保存return值

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

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

使用元组时我更喜欢的更清晰方式的示例:

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

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