C#函数的参数传递2(ref\out)

2021-03-21 11:26

阅读:638

标签:fir   mat   hang   parse   line   turn   lin   src   loading   

using System;
namespace class1
{
    class program
    {
        static void Main(string[] args)
        {
            Console.Write("请输入a=");
            double a = double.Parse(Console.ReadLine());
            Console.Write("请输入b=");
            double b = double.Parse(Console.ReadLine());
            Console.WriteLine("First");
            Console.Write("\t{0}  {1}", a, b);
            Swap(a,b);
            Console.WriteLine("\nSecond");
            Console.WriteLine("\t{0}  {1}" ,a,b);
            Swap1(ref a, ref b);//实际参数也加上REF
            Console.WriteLine("\nThird");
            Console.WriteLine("\t{0}  {1}", a, b);
            Console.Write("请输入r=");
            double r = double.Parse(Console.ReadLine());
            double s = MianJiZhouChang(r, out double c);//这里的out也可以用ref代替,这个时候要为c赋初值
            Console.WriteLine("面积是{0},周长是{1}",s,c);
        }
        static void Swap(double x, double y) {
            double z = x;
            x = y;
            y = z;
        }
        /// 
        /// 使形式参数变成引用传递,加上REF
        /// 
        /// 
        /// 
        static void Swap1(ref double x, ref double y)
        {
            double z = x;
            x = y;
            y = z;
        }
        /// 
        /// OUT应用
        /// 
        /// 
        /// 
        /// 
        static double MianJiZhouChang(double r,out double ZHOUCHANG) {
            double S = Math.PI * r * r;
            ZHOUCHANG = 2 * Math.PI * r;
            return S;
        }
    }
}

技术图片

 

C#函数的参数传递2(ref\out)

标签:fir   mat   hang   parse   line   turn   lin   src   loading   

原文地址:https://www.cnblogs.com/BruceKing/p/11818992.html


评论


亲,登录后才可以留言!