zoukankan      html  css  js  c++  java
  • 方法 params、ref和out关键字使用

    1、方法的声明

             方法是类成员的一种,也称函数成员。函数成员就是类本身提供特定功能的方法,利用调用类对象的方法,使不同的类之间得以相互沟通,完成所要执行的运算或特定的工作。

             Visual C#中,如果方法没有返回值,其返回类型必须为void。如果方法有返回值,则必须指定其返回的数据类型。

    2、方法的参数

             调用方法时可以给该方法传递一个或多个值。传给方法的值叫做实参,在方法内部,接受实参的变量叫形参。形参在紧跟成方法们名的括号中声明。形参的声明语法与变量的声明语法一样。形参只在括号内部有效。

             声明方法参数时可以通过关键字paramsrefout实现。

    2.1params参数

             Params参数是指在方法的参数数目可变处采用的参数。Params参数必须是一维数组。

    例:

     

    代码
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Text;
     4 
     5 namespace Method
     6 {
     7     class Program
     8     {
     9         static void Main(string[] args)
    10         {
    11             string[] arrNumbers = new string[] { "1""2""3""4""5" };
    12 
    13             ParamsMethod(arrNumbers);
    14         }
    15 
    16         static void ParamsMethod(params string[] arr)
    17         {
    18             //for (int i = 0; i < arr.Length; i++)
    19             //{
    20             //    Console.WriteLine(arr[i]);
    21             //}
    22 
    23             foreach (string str in arr)
    24             {
    25                 Console.WriteLine(str);
    26             }
    27         }
    28     }
    29 }
    30 

    2.2、ref参数

         Ref参数使参数按引用传递。其效果是当控制权传递回调用方法时,在方法中对参数所做的任何修改都将反映在变量中。若要使用ref参数,则方法定义和调用方法都必须显式使用ref关键字。

    例:

     

    代码
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Text;
     4 
     5 namespace Method
     6 {
     7     class Program
     8     {
     9         static void Main(string[] args)
    10         {
    11             int year = 0;
    12             RefMethod(ref year);
    13             Console.WriteLine(year);
    14         } 
    15 
    16         static void RefMethod(ref int year)
    17         {
    18             year = 2010;
    19         }
    20     }
    21 }
    22 
    23 

    2.3、out参数

         Out关键字会导致参数通过引用来传递。这与ref关键字类似,不同之处在于ref要求变量必须在传递之前进行初始化。若要使用out参数,方法定义和调用方法都必须显式使用out关键字。

    例:

     

    代码
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Text;
     4 
     5 namespace Method
     6 {
     7     class Program
     8     {
     9         static void Main(string[] args)
    10         {
    11             int month;
    12             OutMethod(out month);
    13             Console.WriteLine(month);
    14         }
    15 
    16         static void OutMethod(out int month)
    17         {
    18             month = 8;
    19         }
    20     }
    21 }
    22 
  • 相关阅读:
    Proj THUDBFuzz Paper Reading: The Art, Science, and Engineering of Fuzzing: A Survey
    Proj THUDBFuzz Paper Reading: A systematic review of fuzzing based on machine learning techniques
    9.3 付费代理的使用
    11.1 Charles 的使用
    第十一章 APP 的爬取
    10.2 Cookies 池的搭建
    10.1 模拟登录并爬取 GitHub
    11.5 Appium 爬取微信朋友圈
    11.4 Appium 的基本使用
    11.3 mitmdump 爬取 “得到” App 电子书信息
  • 原文地址:https://www.cnblogs.com/libingql/p/1804615.html
Copyright © 2011-2022 走看看