当前位置 : 主页 > 网络编程 > c#编程 >

C# 函数返回多个值的方法详情

来源:互联网 收集:自由互联 发布时间:2023-01-31
目录 引言 1.使用ref参数 2.使用out参数修饰符 3. 使用元组类 4.使用C#7 ValueTuple 5. 使用结构或类 引言 根据 C# 语言规范,不可能从一个方法返回多个值。使用 C# 提供的一些其他功能,我们
目录
  • 引言
  • 1.使用ref参数
  • 2.使用out参数修饰符
  • 3. 使用元组类
  • 4.使用C#7 ValueTuple
  • 5. 使用结构或类

引言

根据 C# 语言规范,不可能从一个方法返回多个值。使用 C# 提供的一些其他功能,我们可以将多个值返回给调用者方法。本文概述了一些可用的替代方法来实现这一点。

1.使用ref参数

我们可以使用 ref 关键字 通过引用将值返回给调用者。我们可以使用它从一个方法中返回多个值,

如下所示:

using System;
public class Example
{
    private static void fun(ref int x, ref int y)
    {
        x = 1;
        y = 2;
    }

    public static void Main()
    {
        int x = 0;
        int y = 0;
 
        fun(ref x, ref y);
 
        Console.WriteLine("x = {0}, y = {1}", x, y);
    }
}
/*
    输出: x = 1, y = 2
*/

请注意, ref 关键字不适用于 AsyncIterator 方法。

2.使用out参数修饰符

out关键字导致参数通过引用传递。它就像 ref 关键字,除了 ref 要求在传递变量之前对其进行初始化。

下面的例子演示了使用 out 参数从方法返回多个值。

using System;
 
public class Example
{
    private static void fun(out int x, out int y)
    {
        x = 1;
        y = 2;
    }
 
    public static void Main()
    {
        int x = 0;
        int y = 0;
 
        fun(out x, out y);
 
        Console.WriteLine("x = {0}, y = {1}", x, y);
    }
}
 
/*
    输出: x = 1, y = 2
*/

请注意, out 参数不适用于 Async 和 Iterator 方法。

3. 使用元组类

一个 tuple 是一种数据结构,可让您轻松地将多个值打包到单个对象中。元组通常用于从方法返回多个值。

下面的示例创建一个 2 元组并从 fun() 方法:

using System;
 
public class Example
{
    private static Tuple<int, int> fun() {
        return Tuple.Create(1, 2);
    }
 
    public static void Main()
    {
        Tuple<int, int> tuple = fun();
 
        Console.WriteLine("x = {0}, y = {1}", tuple.Item1, tuple.Item2);
    }
}
 
/*
    输出: x = 1, y = 2
*/

tuple是一个元组,最多支持7个元素,再多需要嵌套等方法实现。

使用元组定义函数的方法如下:

public static Tuple<string,string> TupleFun()
        {
            string[] T = {'hello','world'};
            Tuple<string, string> tup = new Tuple<string, string>(T[0], T[1]);
            return tup;
        }

元组还支持多种类型的值。

public static Tuple<string,int> TupleFun()
        {
            string T = ‘hello';
            int q = 6;
            Tuple<string, int> tup = new Tuple<string, int>(T, q);
            return tup;
        }

在调用函数时,使用Item*来调用元组内的元素。

var tuple = TupleFun();
print(tuple.Item1);
print(int.Parse(tuple.Item2));

4.使用C#7 ValueTuple

值元组,在 .NET Framework 4.7 中引入,是元组类型,用于在 C# 中提供元组的运行时实现。像元组类一样,我们可以使用它以更有效的方式从方法中返回多个值。

下面的示例使用类型推断来解构该方法返回的 2 元组。

using System;
 
public class Example
{
    private static (int, int) fun() {
        return (1, 2);
    }
 
    public static void Main()
    {
        (int x, int y) = fun();
 
        Console.WriteLine("x = {0}, y = {1}", x, y);
    }
}
 
/*
    输出: x = 1, y = 2
*/

5. 使用结构或类

在这里,想法是返回一个包含我们想要返回的所有字段的类的实例。以下代码示例从使用 struct 的方法返回多个值。

using System;
 
public class Example
{
    private struct Pair
    {
        public int x;
        public int y;
    }
 
    private static Pair fun() {
        return new Pair { x = 1, y = 2 };
    }
 
    public static void Main()
    {
        Pair pair = fun();
 
        Console.WriteLine("x = {0}, y = {1}", pair.x, pair.y);
    }
}
 
/*
    输出: x = 1, y = 2
*/

这就是从 C# 中的方法返回多个值的全部内容。

到此这篇关于C# 函数返回多个值的方法详情的文章就介绍到这了,更多相关C# 函数返回值内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!

上一篇:C#Winform分页功能的实现
下一篇:没有了
网友评论