一、使用元组实现
1.1 使用ValueTuple实现
ValueTuple命名为元组(在C#7.1中可用),优点是它最简洁,不可变且易于构造。
private (double first, double second) GetHeight() { return (1,2); } var result = GetHeight(); Console.WriteLine($"{result.first}, {result.second}"); var (first, second) = GetHeight(); Console.WriteLine($"{first}, {second}");
1.2 使用Tuple实现
.NET Framework已经具有通用的Tuple类。但是,这些类有两个主要限制。首先,Tuple类将其属性命名为Item1,Item2,依此类推。这些名称不包含语义信息。使用这些元组类型不能实现传达每个属性的含义。新的语言功能使您可以为元组中的元素声明和使用语义上有意义的名称。
public Tuple<int, int> ViaClassicTuple() { return new Tuple<int, int>(1,2); } var tuple = ViaClassicTuple(); Console.WriteLine($"{tuple.Item1}, {tuple.Item2}");
二、使用列表(list<T>)或数组实现
2.1 使用List列表实现
private List<double> GetHeight() { return new List<double>(){1,2}; } var result = GetHeight(); Console.WriteLine($"{result[0]}, {result[1]}");
2.2 使用数组实现
private double[] GetHeight() { return new double[2]{ 1,2}; } var result = GetHeight(); Console.WriteLine($"{result[0]}, {result[1]}");
三、使用类、结构体实现
最常见的使用方式。
3.1 类实现
public struct ClassicStruct { public int First { get; set; } public int Second { get; set; } public ClassicStruct(int first, int second) { First = first; Second = second; } } public ClassicStruct ViaClassicStruct() { return new ClassicStruct(1, 2); } var classicStruct = ViaClassicStruct(); Console.WriteLine($"{classicStruct.First}, {classicStruct.Second}");
3.2 结构体实现
public class SomeClass { public int First { get; set; } public int Second { get; set; } public SomeClass(int first, int second) { First = first; Second = second; } } public SomeClass ViaSomeClass() { return new SomeClass(1, 2); } var someClass = ViaSomeClass(); Console.WriteLine($"{someClass.First}, {someClass.Second}");
四、使用out参数实现
参数进行的任何操作都是在自变量上进行的。就像ref关键字一样,除了ref要求在传递变量之前先对其进行初始化。它也类似于in关键字,除了in不允许调用的方法修改参数值。要使用out参数,方法定义和调用方法都必须显式使用out关键字。
4.1 多个out参数实现
public bool ViaOutParams(out int first, out int second) { first = 1; second = 2; return someCondition; } if(ViaOutParams(out var firstInt, out var secondInt)) Console.WriteLine($"{firstInt}, {secondInt}");
4.2使用out ValueTuple实现
public bool ViaOutTuple(out (int first,int second) output) { output = (1, 2); return someCondition; } if (ViaOutTuple(out var output)) Console.WriteLine($"{output.first}, {output.second}");
4.3 用out关键字
using System; namespace test { class Testout { public int getParts(double n, out double frac) { int whole; whole = (int)n; frac = n - whole; //pass fractional小数 part back through frac return whole; //return integer portion 返回整数部分 } } class Useout { static void Main() { Testout Tout = new Testout(); int i; double f; i = Tout.getParts(1234.56789, out f); Console.WriteLine("整数部分:" + i); Console.WriteLine("小数部分:{0:#.###}" , f); Console.WriteLine("小数部分:" + f); Console.ReadKey(); //监听键盘事件,按任意键执行退出 } } } /// <summary> /// Ping命令检测网络是否畅通 /// </summary> /// <param name="urls">URL数据</param> /// <param name="errorCount">ping时连接失败个数</param> /// <returns></returns> public static bool MyPing(string[] urls, out int errorCount) { bool isconnected = true; System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping(); errorCount = 0; try { PingReply pr; for (int i = 0; i < urls.Length; i++) { pr = ping.Send(urls[i]); if (pr.Status != IPStatus.Success) { isconnected = false; errorCount++; Debug.LogFormat("目标服务器{0}不可达,错误计数errorCount={1}", urls[i], errorCount); } // Console.WriteLine("Ping " + urls[i] + " " + pr.Status.ToString()); Debug.Log("Ping " + urls[i] + " " + pr.Status.ToString()); } } catch { isconnected = false; errorCount = urls.Length; } //if (errorCount > 0 && errorCount < 3) // isconn = true; return isconnected; } /// <summary> /// 检测网络连接状态 /// </summary> /// <param name="urls"></param> //public static void CheckServeStatus(string[] urls) public bool CheckNetStatus(string[] urls) { int errCount = 0;//ping时连接失败个数 //if (!LocalConnectionStatus()) //{ // Console.WriteLine("网络异常~无连接"); //} if (!MyPing(urls, out errCount)) { if ((double)errCount / urls.Length >= 0.3) { Debug.Log("网络连接异常"); return false; //Console.WriteLine("网络异常~连接多次无响应"); } else { Debug.Log("网络连接正常"); return true; } } else { Debug.Log("网络连接正常"); return true; //Console.WriteLine("网络正常"); } }
参考博文:https://blog.csdn.net/oopxiajun2011/article/details/119105674