c# 类 返回值解析!!

c# 在函数中创建了两个变量 并且赋值 怎么返回这两个变量的值 可以写代码嘛 文字感觉不是很明白

你的函数是怎么创建变量的,你需要返回变量的值指的是什么形式?看看你写的代码

函数调用的时候传参数 ,参数前加修饰符 out ref 等到函数正常调用就可以了

写个结构体或者class返回。

ref和out你去查查吧

img

img


namespace ConsoleApp1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            var a = 0;
            var b = 0;
            Syn(ref a, ref b);
            Console.WriteLine($"a={a},b={b}");

            Syn2(out int aa,out int bb);
            Console.WriteLine($"a={aa},b={bb}");

            var res = Syn3();
            Console.WriteLine($"a={res.A},b={res.B}");
            Console.ReadLine();
        }

        static void Syn(ref int a,ref int b)
        {
            a = 1 * 100;
            b = 2 * 100;
        }

        static void Syn2(out int a, out int b)
        {
            a = 3 * 100;
            b = 4 * 100;
        }

        static SynClass Syn3()
        {
            var res = new SynClass()
            {
                A = 5 * 100,
                B = 6 * 100
            };

            return res;
        }

    }

    public class SynClass
    {
        public int A { get; set; }
        public int B { get; set; }
    }
}
/C#9.0特性

//直接声明即可使用
var ApplyStatus = new ApplyStatus("01", "01");
//直接声明的,你可以用属性访问

Console.WriteLine($"{ApplyStatus.applyStatus}:{ApplyStatus.applyStatusDisplay}");
//也可以用元组直接解包
var (applyStatu, applyStatusDisplay) = new ApplyStatus("02", "02");
//元组解包后即可直接使用
Console.WriteLine($"{applyStatu}:{applyStatusDisplay}");
Console.ReadKey();
//2022直接使用 record语法糖
record ApplyStatus(string applyStatus, string applyStatusDisplay);

//2 用命名元组也可

//命名元组也行

var ApplyStatus = (applyStatus: "01", applyStatusDisplay: "02");