c# 函数方法基础及练习

三个高级参数,out、ref、params。

out与ref都是地址传递。ref是直接传递实参操作,而out是形参操作,调用时必须提前声明一个实参变量,形参赋地址值给它,形参变实参,即ref是实参进出,而out是形参进去,出来的时候把地址赋值给实参,形参变实参。

一:out用法,

out用来返回不同类型的多个值,同类型的用数组
 //函数的使用不受限于定义的前后顺序

    class Program
    {
        
       //函数该返回什么类型关键字就是什么类型,out是另外附加返回的
        public static int GetMybook(int count, out string message)//函数声明时即定义了形参,message直接用
        {
            message = "你给了我" + count + "本书";
            return count;
        }
        static void Main(string[] args)
        {
            //方法调用时可以直接在函数内声明实参,格式:”out+返回类型+实参变量名称“,
            //实参函数外使用,等同于
            //string me;
            //int b = GetMybook(5, out me); 
            int b = GetMybook(5, out string me);
            Console.WriteLine(b);
            Console.WriteLine(me);
            Console.ReadLine();
        }
    }

  //out的用法练习,用来返回多类型返回值。
    class Program
    {
        static void Main(string[] args)
        {

            bool state = MmChecker("yaoyue", 8888888, out string mes);//mes为实参,声明
            Console.WriteLine("登陆状态为{0},信息为:{1}", state, mes);
            Console.ReadLine();
        }
       
        public static bool MmChecker(string names,int passwords,out string message)//message为形参,声明
        {
            
            if ( names == "yaoyue" && passwords ==88888888)
            {
                message = "登陆成功!";
                return true;
            }
            else 
            {
                message = "登陆失败!";
                return false;

            }

        }

    }

  二:ref 的用法

 

!实参与形参:参考( https://zhidao.baidu.com/question/1539776001473384027.html)

ref 关键字会通过引用传递参数,而不是值。
 通过引用传递的效果是在方法中对参数的任何改变都会反映在调用方的基础参数中。
 引用参数的值与基础参数变量的值始终是一样的。类似指针

 

 //形参的调用完成后即销毁,所有函数类型没有返回值就没有意义。除非传递指针进去
    class Program
    {
        static void Main(string[] args)
        {
            //int类型的形参测试
            int b = 10;
            M1(b);
            Console.WriteLine(b);//仍旧是10,如果是M1(ref b)调用,且函数相应的为public static void M1(ref int a){},则结果在函数内部直接修改

            string c = "abc";
            M2(ref c);
            Console.WriteLine(c);//abc123,ref地址传递

            Console.ReadLine();
        }
       
        public static void M1(int a)
        {
            a += 5;

        }
        public static void M2( ref string c)
        {
            c = c + "123" ;

        }
    }

 三,params的用法,params必须是形参的最后一个才行,且仅能有一个params

 class Program
    {
        static void Main(string[] args)
        {
            int[] c = { 1, 2, 3, 4, 5 };
            heel("yaoyue",c);//第一种写法
            heel("yaoyue", 1, 2,3, 4, 5);//第二种写法,提示params可以用同类型数组的单个元素填充,用,隔开即可

        }
       
        public static void heel( string name ,params int[] a)//params 必须是一堆数组
        {
            for (int i = 0; i < a.Length; i++)
            {
                Console.WriteLine(a[i]);
            }
        }    
    }

  四,方法的重载

方法名相同,实现的功能不同,算是一种偷懒方法,函数名字相同且形参数和类型不能同时相同,返回值类型与重载没关系

 上面为共有4个重载方法

 

 五、方法的递归,退出时:怎么一个个进来怎么一个个出去

最常见的例子如:一层层遍历递归子目录列出所有文件名

    class Program
    {//无限循环讲故事的递归
        static void Main(string[] args)
        {
            TellStory();
        }    
        public static void TellStory()
        {
            Console.WriteLine("1从前有座山,山里有个老和尚");
            Console.WriteLine("2老和尚给小和尚讲故事,说");
            TellStory();
        }   
    }
    }

  

 class Program
    {//附带循环次数的递归运算,传形参的方法
        static void Main(string[] args)
        {
            TellStory(0);
            Console.ReadLine();
        }    
        public static void TellStory(int count=0) //缺省值为0
        {
            Console.WriteLine("1从前有座山,山里有个老和尚");
            Console.WriteLine("2老和尚给小和尚讲故事,说");
            count++;
            if (count>10)
            {
                return;
            }
            else 
            {
                TellStory(count);//这个地方注意,count为继续次数
            }

        }   
    }

  

class Program
    {//附带循环次数的递归运算, 静态字段的方法,
        static void Main(string[] args)
        {
            TellStory();
            Console.ReadLine();
        }

        public static int a = 0; //声明一个program类中的静态字段,不用static则必须用program.a ,静态字段作用整个类,不属于实例,引用非静态的【类名.变量名】是必须的
        public static void TellStory() //缺省值为0
        {
            Console.WriteLine("1从前有座山,山里有个老和尚");
            Console.WriteLine("2老和尚给小和尚讲故事,说");
            a++;
            if (a>5)
            {
                return;
            }
            else 
            {
                TellStory();//因为有全局静态字段统计次数,所有不再添加次数
            }

        }   
    }

  

 练习: 1.  输入最大最小值求中间数的和,带用户输入合法性验证

class Program
    {
        static void Main(string[] args)
        {
            int[] x = GetNmbers();
            SumNumbers(x);
            Console.ReadLine();
        }

        /// <summary>
        /// 返回用户输入的2个数字,最大值与最小值,不合法则循环
        /// </summary>
        /// <returns>输入数字组成的数组</returns>
        public static int[] GetNmbers()
        {
            int[] numbers = new int[2];
            while (true)//只要不清楚次数的一律用while
            {
                try
                {
                    Console.WriteLine("请输入最小值:");
                    string a = Console.ReadLine();
                    numbers[0] = Convert.ToInt32(a);
                    Console.WriteLine("请输入最大值:");
                    string b = Console.ReadLine();
                    numbers[1] = Convert.ToInt32(b);
                    return numbers; //return跳出,后面的不执行
      
if (numbers[0] < numbers[1])
{
return numbers;
}
else
{
Console.WriteLine("请确认大小值输入顺序");
}          }
                catch
                {
                    Console.WriteLine("请重新输入合法数字");

                }
            }
        }
        /// <summary>
        /// 计算数组内2个元素最大值与最小值中间数的和
        /// </summary>
        /// <param name="numbers">int数组</param>
        public static void SumNumbers(int[] numbers)
        {

            int sum = 0;

            for (int i = numbers[0]; i < numbers[1]; i++)
            {
                sum += i; //sum=sum+i;  区别sum=++i  先i+1。  sum=i++; 先sum再i++

            }
            Console.WriteLine("和为:" + sum);
            Console.ReadKey();
        }

    }

  

 

 

posted @ 2021-02-02 18:16  遥月  阅读(393)  评论(0)    收藏  举报