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;
            }
           
        }
    }

    程序截图:

  • 相关阅读:
    Handlebars模板引擎简单使用
    SpringMVC学习笔记001-服务器端获取JSON字符串并解析
    EL表达式的使用
    SpringMVC学习笔记001
    ExtJS学习之路碎碎念
    Microsoft Word 使用技巧总结
    驼峰命名法
    视图生命周期
    git命令
    git的使用1[转]
  • 原文地址:https://www.cnblogs.com/xingyunblog/p/3905063.html
Copyright © 2011-2022 走看看