zoukankan      html  css  js  c++  java
  • c#编程基础之ref、out参数

    引例:

    先看这个源码,函数传递后由于传递的是副本所以真正的值并没有改变。

    源码如下:

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace refout参数学习
    {
        class Program
        {
            static void Main(string[] args)
            {
                int age = 20;
                IncAge(age);
                Console.WriteLine(age);//打印结果还是20
                Console.ReadKey();
                
    
            }
            static void IncAge(int age)//复制了一份,所以IncAge内部改变的是副本
            {
                age++;
            }
        }
    }

    运行截图:

    要解决上面方法就需要使用ref参数:

    上面源码修改后如下:

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace refout参数学习
    {
        class Program
        {
            static void Main(string[] args)
            {
                int age = 20;
                IncAge(ref age);//此处必须也加上参数ref
                Console.WriteLine(age);//使用参数ref后,传递真值,而不是副本,所以打印21
                Console.ReadKey();
                
    
            }
            static void IncAge(ref int age)//使用ref参数后,传递过来的将不是副本,而是原本,函数内改变,其值也将发生改变
            {
                age++;
            }
        }
    }

    程序截图:

    out参数由内部进行赋值,所传递参数无需初始化,而且即使初始化也没用。

    源码如下:

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace refout参数学习
    {
        class Program
        {
            static void Main(string[] args)
            {
                int age;
                IncAge(out age);//此处必须也加上参数out
                Console.WriteLine(age);//使用参数out后,传递真值,并有函数内部赋值所以打印22
                Console.ReadKey();
                
    
            }
            static void IncAge(out int age)//使用out参数后,所传递值不需要初始化,由函数内部为其赋值。
            {
                age = 22;
            }
        }
    }

    程序截图:

    out参数应用场景:

    源码如下:

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace refout参数学习
    {
        class Program
        {
            static void Main(string[] args)
            {
                string str = Console.ReadLine();
                int i;
                if(int.TryParse(str,out i))
                {
                    Console.WriteLine("转换成功!{0}",i);
                }
                else
                {
                    Console.WriteLine("数据错误!");
                }
                Console.ReadKey();
            }
           
        }
    }

    运行结果:

    ref运行场景,比如我们先看这个源码:

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace refout参数学习
    {
        class Program
        {
            static void Main(string[] args)
            {
                int i1 = 10;
                int i2 = 20;
                Swap(i1, i2);//将i1和i2各复制一份传递给函数
                Console.WriteLine("i1={0},i2={1}", i1, i2);//i1和i2没有发生交换,还是i1=10,i2=20       
                Console.ReadKey();
            }
            static void Swap(int i1, int i2) //交换函数
            {
                int temp=i1;
                i1 = i2;
                i2 = temp;
            }
           
        }
    }

    运行结果:

    想要实现真的交换,这时就需要ref参数了,修改源码如下:

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace refout参数学习
    {
        class Program
        {
            static void Main(string[] args)
            {
                int i1 = 10;
                int i2 = 20;
                Swap(ref i1,ref i2);//将i1和i2传递给函数
                Console.WriteLine("i1={0},i2={1}", i1, i2);//i1和i2发生交换,还是i1=20,i2=10     
                Console.ReadKey();
            }
            static void Swap(ref int i1,ref int i2) //接受传递来的i1和i2的值,将i1和i2的值进行交换。
            {
                int temp=i1;
                i1 = i2;
                i2 = temp;
            }
           
        }
    }

    程序截图:

  • 相关阅读:
    python 安装pillow
    rapidminer 数据导入及几个算子简单应用
    Fiddler高级用法-设置断点
    Linux 操作MySQL常用命令行
    VMWare虚拟机提示:另一个程序已锁定文件的一部分,打不开磁盘...模块"Disk"启动失败的解决办法
    解决VMware15 centos7 桥接模式ssh突然不能访问的问题
    新媒体运营需要什么能力?需要具备哪些运营技能?
    运营分为哪几类?具体的工作职责是什么?
    Centos7 yum安装MySQL5.7.25
    Centos7防火墙添加端口
  • 原文地址:https://www.cnblogs.com/xingyunblog/p/3905063.html
Copyright © 2011-2022 走看看