簡體   English   中英

一次從同一方法獲取多個值時的更好方法

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

我有以下返回元組的方法,

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

稱為,

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

這工作正常,但我不喜歡我兩次調用相同的方法來獲取 item1 和 item2,使用 Tuple 可能不是前進的方向,但如果 c# 支持在一次執行該方法時返回兩個值,任何建議?

你只需要保存返回值

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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM