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

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值