zoukankan      html  css  js  c++  java
  • ref out params

    ref侧重于将一个值传入函数进行处理,再将处理后的值返还回去;参数是在函数外进行赋值。--将值类型传递转换成引用类型传递

    out侧重于函数需要返回多个值;参数是在函数内进行赋值,函数外只进行声明数据类型。

    params可变参数

    out实例:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Diagnostics;
     4 using System.IO;
     5 using System.Linq;
     6 using System.Text;
     7 using System.Threading.Tasks;
     8 
     9 namespace ConsoleApplication4
    10 {
    11     class Program
    12     {
    13         static void Main(string[] args)
    14         {
    15             Console.WriteLine("请输入账号");
    16             string uname = Console.ReadLine();
    17             Console.WriteLine("请输入密码");
    18             string upwd = Console.ReadLine();
    19             string uresult = string.Empty;//只声明数据类型,只要保证“形参”里面是该变量名,系统会自动赋值
    20             bool a = login(uname,upwd, out uresult);//注意out
    21             Console.WriteLine("登录状态为{0},因为{1}",a.ToString(),uresult);
    22             Console.ReadKey();
    23         }
    24         //根据账号密码登录返回不一样的提示。注意此处是静态函数
    25         static bool login(string name, string pwd, out string result)//返回bool和out内的string类型
    26         {
    27 
    28             bool b;
    29             if (name == "admin" && pwd == "123")
    30             {
    31                 result = "登陆成功";
    32                 b = true;
    33             }
    34             else if (name == "123")
    35             {
    36                 result = "密码不对";
    37                 b = false;
    38             }
    39             else if (pwd == "123")
    40             {
    41                 result = "账号不对";
    42                 b = false;
    43             }
    44             else
    45             {
    46                 result = "账户密码不对";
    47                 b = false;
    48             }
    49             return b;
    50 
    51         }
    52     }
    53 }
    View Code

    ref实例:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Diagnostics;
     4 using System.IO;
     5 using System.Linq;
     6 using System.Text;
     7 using System.Threading.Tasks;
     8 
     9 namespace ConsoleApplication4
    10 {
    11     class Program
    12     {
    13         static void Main(string[] args)
    14         {
    15             int a = 100;
    16             bian1(a);
    17             Console.WriteLine(a);//此时a为100
    18 
    19             int b = 100;
    20             bian2(ref b);//必须有ref,关键字
    21             Console.WriteLine(b);//此处b为300。ref将b进行处理,然后在返还回来
    22 
    23             int c = 100;
    24             int cc = bian3(ref c);
    25             Console.WriteLine(cc);//cc为500
    26             Console.WriteLine(c);//c为400
    27 
    28             Console.ReadKey();
    29         }
    30         static void bian1(int aa)//普通函数
    31         {
    32             aa = 200;
    33         }
    34         static void bian2(ref int aa)//注意ref,同是函数还有返回值
    35         {
    36             aa = 300;
    37         }
    38         static int bian3(ref int aa)
    39         {
    40             aa = 400;//只对声明的ref变量进行处理后再返还回去
    41             return 500;//跟ref aa变量没关系
    42         }
    43     }
    44 }
    View Code

    params实例:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Diagnostics;
     4 using System.IO;
     5 using System.Linq;
     6 using System.Text;
     7 using System.Threading.Tasks;
     8 
     9 namespace ConsoleApplication4
    10 {
    11     class Program
    12     {
    13         static void Main(string[] args)
    14         {
    15             //params可变参数,可以放数组,也可以放跟数组数据类型一样的数值,但是params必须设置在最后一个参数位置,因为如果不在最后,系统对应不到相应的参数
    16             aa("name", new int[] { 1, 2, 3, 4 });
    17             aa("name",1,2,3,4);//两种方法都可以,但是
    18         }
    19         static void aa(string name,params int[] aa)//声明一个函数,
    20         {
    21             Console.WriteLine("aa");
    22         }
    23        
    24     }
    25 }
    View Code

    完!

  • 相关阅读:
    javaweb
    反射 day1
    JDBC-day1
    总结
    day5
    day4
    day3
    18.10.17 考试总结
    洛谷P2172 [bzoj] 2150 部落战争
    18.10.15 考试总结
  • 原文地址:https://www.cnblogs.com/wwz-wwz/p/6388333.html
Copyright © 2011-2022 走看看