.NET ez 2017-01-13
一般如果要回傳多個值會建立一個 class,然後再使用class方式回傳。
例如:
public class Info { public int UserID { get; set; } public string UserName { get; set; } public bool UserSex { get; set; } } public Info GetInfo() { return new Info { UserID = 1, UserName = "小明", UserSex = true }; }
也可以使用 Tuple 方式進行回傳,但是此方式不易辨識值的名稱,可能會造成程式混亂,所以自行判斷是否使用。
public static Tuple<int, string, bool> TupleGetInfo() { return new Tuple<int, string, bool>(1, "小明", true); } private static void Main(string[] args) { Tuple<int, string, bool> Info = TupleGetInfo(); int UserID = Info.Item1; string UserName = Info.Item2; bool UserSex = Info.Item3; Console.ReadLine(); }
標籤: .NET
本文章網址:
https://www.ez2o.com/Blog/Post/csharp-tuple-Return-Multiple-Values
https://www.ez2o.com/Blog/Post/588
https://www.ez2o.com/Blog/Post/csharp-tuple-Return-Multiple-Values
https://www.ez2o.com/Blog/Post/588