• C#基础入门教程-方法


    目录

    C#方法

    C# 中定义方法

    实例练习

    C# 中调用方法

    实例练习

    实例练习

    递归方法调用

    实例练习

    参数传递

    按值传递参数

    实例练习

    按引用传递参数

    实例练习

    按输出传递参数

    实例练习

    实例练习


    C#方法

    一个方法是把一些相关的语句组织在一起,用来执行一个任务的语句块。每一个 C# 程序至少有一个带有 Main 方法的类。

    要使用一个方法,您需要:

    • 定义方法
    • 调用方法

    C# 中定义方法

    当定义一个方法时,从根本上说是在声明它的结构的元素。在 C# 中,定义方法的语法如下:

              (Parameter List)

            {

                       Method Body

            }

    下面是方法的各个元素:

    • Access Specifier:访问修饰符,这个决定了变量或方法对于另一个类的可见性。
    • Return type:返回类型,一个方法可以返回一个值。返回类型是方法返回的值的数据类型。如果方法不返回任何值,则返回类型为 void
    • Method name:方法名称,是一个唯一的标识符,且是大小写敏感的。它不能与类中声明的其他标识符相同。
    • Parameter list:参数列表,使用圆括号括起来,该参数是用来传递和接收方法的数据。参数列表是指方法的参数类型、顺序和数量。参数是可选的,也就是说,一个方法可能不包含参数。
    • Method body:方法主体,包含了完成任务所需的指令集。

    下面的代码片段显示一个函数 FindMax,它接受两个整数值,并返回两个中的较大值。它有 public 访问修饰符,所以它可以使用类的实例从类的外部进行访问。

    实例练习

    1. class NumberManipulator
    2. {
    3.    public int FindMax(int num1, int num2)
    4.    {
    5.       /* 局部变量声明 */
    6.       int result;
    7.       if (num1 > num2)
    8.          result = num1;
    9.       else
    10.          result = num2;
    11.       return result;
    12.    }
    13.    ...
    14. }

    C# 中调用方法

    您可以使用方法名调用方法。下面的实例演示了这点:

    实例练习

    1. using System;
    2. namespace CalculatorApplication
    3. {
    4.    class NumberManipulator
    5.    {
    6.       public int FindMax(int num1, int num2)
    7.       {
    8.          /* 局部变量声明 */
    9.          int result;
    10.          if (num1 > num2)
    11.             result = num1;
    12.          else
    13.             result = num2;
    14.          return result;
    15.       }
    16.       static void Main(string[] args)
    17.       {
    18.          /* 局部变量定义 */
    19.          int a = 100;
    20.          int b = 200;
    21.          int ret;
    22.          NumberManipulator n = new NumberManipulator();
    23.          //调用 FindMax 方法
    24.          ret = n.FindMax(a, b);
    25.          Console.WriteLine("最大值是: {0}", ret );
    26.          Console.ReadLine();
    27.       }
    28.    }
    29. }

    当上面的代码被编译和执行时,它会产生下列结果:

            最大值是: 200

    您也可以使用类的实例从另一个类中调用其他类的公有方法。例如,方法 FindMax 属于 NumberManipulator 类,您可以从另一个类 Test 中调用它。

    实例练习

    1. using System;
    2. namespace CalculatorApplication
    3. {
    4.     class NumberManipulator
    5.     {
    6.         public int FindMax(int num1, int num2)
    7.         {
    8.             /* 局部变量声明 */
    9.             int result;
    10.             if (num1 > num2)
    11.                 result = num1;
    12.             else
    13.                 result = num2;
    14.             return result;
    15.         }
    16.     }
    17.     class Test
    18.     {
    19.         static void Main(string[] args)
    20.         {
    21.             /* 局部变量定义 */
    22.             int a = 100;
    23.             int b = 200;
    24.             int ret;
    25.             NumberManipulator n = new NumberManipulator();
    26.             //调用 FindMax 方法
    27.             ret = n.FindMax(a, b);
    28.             Console.WriteLine("最大值是: {0}", ret );
    29.             Console.ReadLine();
    30.         }
    31.     }
    32. }

    当上面的代码被编译和执行时,它会产生下列结果:

            最大值是: 200

    递归方法调用

    一个方法可以自我调用。这就是所谓的 递归。下面的实例使用递归函数计算一个数的阶乘:

    实例练习

    1. using System;
    2. namespace CalculatorApplication
    3. {
    4.     class NumberManipulator
    5.     {
    6.         public int factorial(int num)
    7.         {
    8.             /* 局部变量定义 */
    9.             int result;
    10.             if (num == 1)
    11.             {
    12.                 return 1;
    13.             }
    14.             else
    15.             {
    16.                 result = factorial(num - 1) * num;
    17.                 return result;
    18.             }
    19.         }
    20.     
    21.         static void Main(string[] args)
    22.         {
    23.             NumberManipulator n = new NumberManipulator();
    24.             //调用 factorial 方法
    25.             Console.WriteLine("6 的阶乘是: {0}", n.factorial(6));
    26.             Console.WriteLine("7 的阶乘是: {0}", n.factorial(7));
    27.             Console.WriteLine("8 的阶乘是: {0}", n.factorial(8));
    28.             Console.ReadLine();
    29.         }
    30.     }
    31. }

    当上面的代码被编译和执行时,它会产生下列结果:

            6 的阶乘是: 720

            7 的阶乘是: 5040

            8 的阶乘是: 40320

    参数传递

    当调用带有参数的方法时,您需要向方法传递参数。在 C# 中,有三种向方法传递参数的方式:

    方式描述
    值参数

    这种方式复制参数的实际值给函数的形式参数,实参和形参使用的是两个不同内存中的值。在这种情况下,当形参的值发生改变时,不会影响实参的值,从而保证了实参数据的安全。

    引用参数

    这种方式复制参数的内存位置的引用给形式参数。这意味着,当形参的值发生改变时,同时也改变实参的值。

    输出参数

    这种方式可以返回多个值。

    值传递参数

    这是参数传递的默认方式。在这种方式下,当调用一个方法时,会为每个值参数创建一个新的存储位置。

    实际参数的值会复制给形参,实参和形参使用的是两个不同内存中的值。所以,当形参的值发生改变时,不会影响实参的值,从而保证了实参数据的安全。下面的实例演示了这个概念:

    实例练习

    1. using System;
    2. namespace CalculatorApplication
    3. {
    4.    class NumberManipulator
    5.    {
    6.       public void swap(int x, int y)
    7.       {
    8.          int temp;
    9.          
    10.          temp = x; /* 保存 x 的值 */
    11.          x = y;    /* 把 y 赋值给 x */
    12.          y = temp; /* 把 temp 赋值给 y */
    13.       }
    14.       
    15.       static void Main(string[] args)
    16.       {
    17.          NumberManipulator n = new NumberManipulator();
    18.          /* 局部变量定义 */
    19.          int a = 100;
    20.          int b = 200;
    21.          
    22.          Console.WriteLine("在交换之前,a 的值: {0}", a);
    23.          Console.WriteLine("在交换之前,b 的值: {0}", b);
    24.          
    25.          /* 调用函数来交换值 */
    26.          n.swap(a, b);
    27.          
    28.          Console.WriteLine("在交换之后,a 的值: {0}", a);
    29.          Console.WriteLine("在交换之后,b 的值: {0}", b);
    30.          
    31.          Console.ReadLine();
    32.       }
    33.    }
    34. }

    当上面的代码被编译和执行时,它会产生下列结果:

            在交换之前,a 的值:100

            在交换之前,b 的值:200

            在交换之后,a 的值:100

            在交换之后,b 的值:200

    结果表明,即使在函数内改变了值,值也没有发生任何的变化。

    按引用传递参数

    引用参数是一个对变量的内存位置的引用。当按引用传递参数时,与值参数不同的是,它不会为这些参数创建一个新的存储位置。引用参数表示与提供给方法的实际参数具有相同的内存位置。

    在 C# 中,使用 ref 关键字声明引用参数。下面的实例演示了这点:

    实例练习

    1. using System;
    2. namespace CalculatorApplication
    3. {
    4.    class NumberManipulator
    5.    {
    6.       public void swap(ref int x, ref int y)
    7.       {
    8.          int temp;
    9.          temp = x; /* 保存 x 的值 */
    10.          x = y;    /* 把 y 赋值给 x */
    11.          y = temp; /* 把 temp 赋值给 y */
    12.        }
    13.    
    14.       static void Main(string[] args)
    15.       {
    16.          NumberManipulator n = new NumberManipulator();
    17.          /* 局部变量定义 */
    18.          int a = 100;
    19.          int b = 200;
    20.          Console.WriteLine("在交换之前,a 的值: {0}", a);
    21.          Console.WriteLine("在交换之前,b 的值: {0}", b);
    22.          /* 调用函数来交换值 */
    23.          n.swap(ref a, ref b);
    24.          Console.WriteLine("在交换之后,a 的值: {0}", a);
    25.          Console.WriteLine("在交换之后,b 的值: {0}", b);
    26.  
    27.          Console.ReadLine();
    28.       }
    29.    }
    30. }

    当上面的代码被编译和执行时,它会产生下列结果:

            在交换之前,a 的值:100

            在交换之前,b 的值:200

            在交换之后,a 的值:200

            在交换之后,b 的值:100

    结果表明,swap 函数内的值改变了,且这个改变可以在 Main 函数中反映出来。

    按输出传递参数

    return 语句可用于只从函数中返回一个值。但是,可以使用 输出参数 来从函数中返回两个值。输出参数会把方法输出的数据赋给自己,其他方面与引用参数相似。

    下面的实例演示了这点:

    实例练习

    1. using System;
    2. namespace CalculatorApplication
    3. {
    4.    class NumberManipulator
    5.    {
    6.       public void getValue(out int x )
    7.       {
    8.          int temp = 5;
    9.          x = temp;
    10.       }
    11.    
    12.       static void Main(string[] args)
    13.       {
    14.          NumberManipulator n = new NumberManipulator();
    15.          /* 局部变量定义 */
    16.          int a = 100;
    17.          
    18.          Console.WriteLine("在方法调用之前,a 的值: {0}", a);
    19.          
    20.          /* 调用函数来获取值 */
    21.          n.getValue(out a);
    22.          Console.WriteLine("在方法调用之后,a 的值: {0}", a);
    23.          Console.ReadLine();
    24.       }
    25.    }
    26. }

    当上面的代码被编译和执行时,它会产生下列结果:

            在方法调用之前,a 的值: 100

            在方法调用之后,a 的值: 5

    提供给输出参数的变量不需要赋值。当需要从一个参数没有指定初始值的方法中返回值时,输出参数特别有用。请看下面的实例,来理解这一点:

    实例练习

    1. using System;
    2. namespace CalculatorApplication
    3. {
    4.    class NumberManipulator
    5.    {
    6.       public void getValues(out int x, out int y )
    7.       {
    8.           Console.WriteLine("请输入第一个值: ");
    9.           x = Convert.ToInt32(Console.ReadLine());
    10.           Console.WriteLine("请输入第二个值: ");
    11.           y = Convert.ToInt32(Console.ReadLine());
    12.       }
    13.    
    14.       static void Main(string[] args)
    15.       {
    16.          NumberManipulator n = new NumberManipulator();
    17.          /* 局部变量定义 */
    18.          int a , b;
    19.          
    20.          /* 调用函数来获取值 */
    21.          n.getValues(out a, out b);
    22.          Console.WriteLine("在方法调用之后,a 的值: {0}", a);
    23.          Console.WriteLine("在方法调用之后,b 的值: {0}", b);
    24.          Console.ReadLine();
    25.       }
    26.    }
    27. }

    当上面的代码被编译和执行时,它会产生下列结果(取决于用户输入):

            请输入第一个值:

            7

            请输入第二个值:

            8

            在方法调用之后,a 的值: 7

            在方法调用之后,b 的值: 8

  • 相关阅读:
    els_ 画矩形、代码规划和备份
    Matlab数据导入代码(importdata函数允许加载不同格式的各种数据文件)
    Linux Shell脚本的10个有用的“面试问题和解答”
    Go并发可视化解释 – select语句
    3分钟学会设计模式 -- 单例模式
    gitlab-rake gitlab:backup:create 执行报错 Errno::ENOSPC: No space left on device
    鸿蒙开发—基础组件
    带你学会指针进阶
    什么是Java中的代理模式?
    skywalking源码--agent配置加载
  • 原文地址:https://blog.csdn.net/qq_40660998/article/details/126950411