unity3D-游戏/AR/VR在线就业班 C#入门方法参数学习笔记

来源:互联网 发布:淘宝联邦止咳水暗语 编辑:程序博客网 时间:2024/04/29 19:05
unity3D-游戏/AR/VR在线就业班 C#入门方法参数学习笔记
点击观看视频学习:http://edu.csdn.NET/lecturer/107

一、方法参数

定义方法时,在参数列表中定义的参数叫做形参;调用方法时,在参数列表中传递的参数叫做实参;

namespace Lesson_09{    public  class  MyClass{        public  void  Swap(int a,int b){                        int temp = a;            a = b;            b = temp;        }        public  void  A(int[] arr){            arr[0]=12;                    }    }    class MainClass    {        public static void Main (string[] args)        {            MyClass c = new MyClass ();            int [] a=new int[]{1};            c.A (a);            Console.WriteLine (a[0]);            }    }}

引用参数

值类型参数想要达到引用类型参数的效果,需要用到引用修饰符ref

using System;namespace Lesson_09{    public  class  MyClass{        public  void  Swap(ref int a, ref int b){                        int temp = a;            a = b;            b = temp;        }        //如果想在方法内对参数进行修改来影响外部的值,那么可以使用引用类型的参数//        public  void  A(int[] arr){//            arr[0]=12;            //        }    }    class MainClass    {        public static void Main (string[] args)        {            MyClass c = new MyClass ();//            int [] a=new int[]{1};//            c.A (a);//            Console.WriteLine (a[0]);                int i=5;            int j = 12;            c.Swap (ref i,ref j);            Console.WriteLine ("i="+i);            Console.WriteLine ("j="+j);        }    }}

输出参数

输出参数:如果想让一个方法返回多个值,可以用输出参数来处理,在参数钱加一个out修饰符

using System;namespace Lesson_09{    public  class  MyClass{        public  int Max(int a,int b,int c){                        int max = a;            if(max<b){                max = b;                            }            if(max<c){                max = c;            }            return max;        }    }    class MainClass    {        public static void Main (string[] args)        {            MyClass c = new MyClass ();            int i = 3;            int j = -4;            int k = 14;            Console.WriteLine (c.Max(i,j,k));        }    }}

对比输出最小值:使用out修饰符

using System;namespace Lesson_09{    public  class  MyClass{        //使用out修饰符来定义输出参数        public  int Max(int a,int b,int c,out int min){            //初始化输出参数            min = 0;            int m = a;            if(m>b){                m = b;            }            if(m>c){                m = c;            }            min = m;            int max = a;            if(max<b){                max = b;                            }            if(max<c){                max = c;            }            return max;        }                }    class MainClass    {        public static void Main (string[] args)        {            MyClass c = new MyClass ();            int i = 3;            int j = -4;            int k = 14;            int min = 0;            int max = c.Max (i, j, k,out min);            Console.WriteLine ("Min="+min);        }    }}




0 0
原创粉丝点击