C# 方法的定义,调用,和参数传递(学习心得 12)

用来执行一个任务的语句块。

每个 C# 程序至少有一个 Main 方法。

使用方法需要:

  • 定义方法。
  • 调用方法。

超级小白友好,讲解C#基础,每集5分钟轻松学习,拒绝从入门到放弃!

一、定义方法

语法:

<Access Specifier> <Return Type> <Method Name>(Parameter List)
{
   Method Body
}

参数:

  • Access Specifier:访问修饰符。
  • Return type:返回类型,一个方法可以返回一个值。返回类型是方法返回的值的数据类型。如果方法不返回任何值,则返回类型为 void
  • Method name:方法名称,大小写敏感。不能与 类 中其他标识符相同。
  • Parameter list:参数列表,使用圆括号括起来。一个方法可能不包含参数。
  • Method body:方法主体,指令集。

例(比大小方法):

class NumberManipulator
{
   public int FindMax(int num1, int num2)
   {     
      int result; // 局部变量声明

      if (num1 > num2)
         result = num1;
      else
         result = num2;

      return result;
   }
   ...
}

二、调用方法

例:调用类自身的方法

using System;

namespace CalculatorApplication
{
   class NumberManipulator
   {
      public int FindMax(int num1, int num2)
      {
         int result;

         if (num1 > num2)
            result = num1;
         else
            result = num2;

         return result;
      }
      static void Main(string[] args)
      {
         int a = 100;
         int b = 200;
         int ret;
         NumberManipulator n = new NumberManipulator(); // 类的实例化
        
         ret = n.FindMax(a, b); //调用 类的FindMax 方法
         Console.WriteLine("最大值是: {0}", ret );
         Console.ReadLine();
      }
   }
}

运行结果:

最大值是: 200

例:调用别的类下的方法

using System;

namespace CalculatorApplication
{
    class NumberManipulator
    {
        public int FindMax(int num1, int num2)
        {
            int result;

            if (num1 > num2)
                result = num1;
            else
                result = num2;

            return result;
        }
    }
    class Test
    {
        static void Main(string[] args)
        {
            int a = 100;
            int b = 200;
            int ret;
            NumberManipulator n = new NumberManipulator();
            ret = n.FindMax(a, b); //调用 FindMax 方法
            Console.WriteLine("最大值是: {0}", ret );
            Console.ReadLine();

        }
    }
}

运行结果:

最大值是: 200

三、递归方法调用

用方法进行自我调用,即递归。

例:计算阶乘

using System;

namespace CalculatorApplication
{
    class NumberManipulator
    {
        public int factorial(int num)
        {
            int result;

            if (num == 1)
            {
                return 1; // 跳出递归的条件
            }
            else
            {
                result = factorial(num - 1) * num;
                // 不断调用方法自身,实现递归
                return result;
            }
        }

        static void Main(string[] args)
        {
            NumberManipulator n = new NumberManipulator();
            //调用 factorial 方法
            Console.WriteLine("6 的阶乘是: {0}", n.factorial(6));
            Console.WriteLine("7 的阶乘是: {0}", n.factorial(7));
            Console.WriteLine("8 的阶乘是: {0}", n.factorial(8));
            Console.ReadLine();

        }
    }
}

运行结果:

6 的阶乘是: 720
7 的阶乘是: 5040
8 的阶乘是: 40320

四、参数传递

调用带有参数的方法,需要传递参数。

传递参数的 3 种方式:

  • 值参数:实参和形参,使用不同内存地址,形参发生变化不影响实参,保证实参安全性。
  • 引用参数:实参和形参,使用相同内存地址。
  • 输出参数:这种方式可以返回多个值。

4.1 按值传递参数

参数传递的默认方式。

调用方法时,为每个参数创建一个新的存储位置。

例:

using System;
namespace CalculatorApplication
{
   class NumberManipulator
   {
      public void swap(int x, int y)
      {
         int temp;
         
         temp = x; /* 保存 x 的值 */
         x = y;    /* 把 y 赋值给 x */
         y = temp; /* 把 temp 赋值给 y */
      }
     
      static void Main(string[] args)
      {
         NumberManipulator n = new NumberManipulator();
         /* 局部变量定义 */
         int a = 100;
         int b = 200;
         
         Console.WriteLine("在交换之前,a 的值: {0}", a);
         Console.WriteLine("在交换之前,b 的值: {0}", b);
         
         /* 调用函数来交换值 */
         n.swap(a, b);
         
         Console.WriteLine("在交换之后,a 的值: {0}", a);
         Console.WriteLine("在交换之后,b 的值: {0}", b);
         
         Console.ReadLine();
      }
   }
}

运行结果:

在交换之前,a 的值:100
在交换之前,b 的值:200
在交换之后,a 的值:100
在交换之后,b 的值:200

4.2 按引用传递参数

对参数的内存位置进行引用。

在 C# 中,使用 ref 关键字声明引用参数。

例:

using System;
namespace CalculatorApplication
{
   class NumberManipulator
   {
      public void swap(ref int x, ref int y)
      {
         int temp;

         temp = x; /* 保存 x 的值 */
         x = y;    /* 把 y 赋值给 x */
         y = temp; /* 把 temp 赋值给 y */
       }
   
      static void Main(string[] args)
      {
         NumberManipulator n = new NumberManipulator();
         /* 局部变量定义 */
         int a = 100;
         int b = 200;

         Console.WriteLine("在交换之前,a 的值: {0}", a);
         Console.WriteLine("在交换之前,b 的值: {0}", b);

         /* 调用函数来交换值 */
         n.swap(ref a, ref b); // 调用的时候也要有 ref

         Console.WriteLine("在交换之后,a 的值: {0}", a);
         Console.WriteLine("在交换之后,b 的值: {0}", b);
 
         Console.ReadLine();

      }
   }
}

运行结果:

在交换之前,a 的值:100
在交换之前,b 的值:200
在交换之后,a 的值:200
在交换之后,b 的值:100

4.3 按输出传递参数

使用输出参数,可以从函数中返回两个值。

输出参数会把方法输出的数据赋给自己,和引用传递参数类似。

例:

using System;

namespace CalculatorApplication
{
   class NumberManipulator
   {
      public void getValue(out int x )
      {
         int temp = 5;
         x = temp;
      }
   
      static void Main(string[] args)
      {
         NumberManipulator n = new NumberManipulator();
         /* 局部变量定义 */
         int a = 100;
         
         Console.WriteLine("在方法调用之前,a 的值: {0}", a);
         
         /* 调用函数来获取值 */
         n.getValue(out a);

         Console.WriteLine("在方法调用之后,a 的值: {0}", a);
         Console.ReadLine();

      }
   }
}

运行结果:

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

提供给输出参数的变量不需要赋值。

当需要从一个参数没有指定初始值的方法中返回值时,输出参数特别有用。

例:

using System;

namespace CalculatorApplication
{
   class NumberManipulator
   {
      public void getValues(out int x, out int y )
      {
          Console.WriteLine("请输入第一个值: ");
          x = Convert.ToInt32(Console.ReadLine());
          Console.WriteLine("请输入第二个值: ");
          y = Convert.ToInt32(Console.ReadLine());
      }
   
      static void Main(string[] args)
      {
         NumberManipulator n = new NumberManipulator();
         /* 局部变量定义 */
         int a , b;
         
         /* 调用函数来获取值 */
         n.getValues(out a, out b);

         Console.WriteLine("在方法调用之后,a 的值: {0}", a);
         Console.WriteLine("在方法调用之后,b 的值: {0}", b);
         Console.ReadLine();
      }
   }
}

运行结果:

请输入第一个值:
3
请输入第二个值:
8
在方法调用之后,a 的值: 3
在方法调用之后,b 的值: 8
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值