VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > temp > 简明python教程 >
  • C#函数的参数传递2(ref\out)

复制代码
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;
        }
        /// <summary>
        /// 使形式参数变成引用传递,加上REF
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        static void Swap1(ref double x, ref double y)
        {
            double z = x;
            x = y;
            y = z;
        }
        /// <summary>
        /// OUT应用
        /// </summary>
        /// <param name="r"></param>
        /// <param name="ZHOUCHANG"></param>
        /// <returns></returns>
        static double MianJiZhouChang(double r,out double ZHOUCHANG) {
            double S = Math.PI * r * r;
            ZHOUCHANG = 2 * Math.PI * r;
            return S;
        }
    }
}
复制代码

相关教程