Csharp 简明教程

C

方法是一组共同执行任务的代码段。每个 C# 程序都至少有一个包含名为 Main 的方法的类。

A method is a group of statements that together perform a task. Every C# program has at least one class with a method named Main.

要在方法中使用,你需要——

To use a method, you need to −

  1. Define the method

  2. Call the method

Defining Methods in C

当你定义方法时,基本上就声明了其结构的元素。C# 中定义方法的语法如下——

When you define a method, you basically declare the elements of its structure. The syntax for defining a method in C# is as follows −

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

以下是方法的各种元素——

Following are the various elements of a method −

  1. Access Specifier − This determines the visibility of a variable or a method from another class.

  2. Return type − A method may return a value. The return type is the data type of the value the method returns. If the method is not returning any values, then the return type is void.

  3. Method name − Method name is a unique identifier and it is case sensitive. It cannot be same as any other identifier declared in the class.

  4. Parameter list − Enclosed between parentheses, the parameters are used to pass and receive data from a method. The parameter list refers to the type, order, and number of the parameters of a method. Parameters are optional; that is, a method may contain no parameters.

  5. Method body − This contains the set of instructions needed to complete the required activity.

Example

以下代码片段展示了一个函数 FindMax,它接收两个整数值并返回较大的一个。它有公共访问说明符,因此可以使用类的实例从类外部访问它。

Following code snippet shows a function FindMax that takes two integer values and returns the larger of the two. It has public access specifier, so it can be accessed from outside the class using an instance of the class.

class NumberManipulator {

   public int FindMax(int num1, int num2) {
      /* local variable declaration */
      int result;

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

      return result;
   }
   ...
}

Calling Methods in C

你可以使用这种名称调用方法。以下示例对此进行说明——

You can call a method using the name of the method. The following example illustrates this −

using System;

namespace CalculatorApplication {
   class NumberManipulator {
      public int FindMax(int num1, int num2) {
         /* local variable declaration */
         int result;

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

      static void Main(string[] args) {
         /* local variable definition */
         int a = 100;
         int b = 200;
         int ret;
         NumberManipulator n = new NumberManipulator();

         //calling the FindMax method
         ret = n.FindMax(a, b);
         Console.WriteLine("Max value is : {0}", ret );
         Console.ReadLine();
      }
   }
}

编译并执行上述代码后,将产生以下结果 −

When the above code is compiled and executed, it produces the following result −

Max value is : 200

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

You can also call public method from other classes by using the instance of the class. For example, the method FindMax belongs to the NumberManipulator class, you can call it from another class Test.

using System;

namespace CalculatorApplication {
   class NumberManipulator {
      public int FindMax(int num1, int num2) {
         /* local variable declaration */
         int result;

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

         return result;
      }
   }
   class Test {
      static void Main(string[] args) {
         /* local variable definition */
         int a = 100;
         int b = 200;
         int ret;
         NumberManipulator n = new NumberManipulator();

         //calling the FindMax method
         ret = n.FindMax(a, b);
         Console.WriteLine("Max value is : {0}", ret );
         Console.ReadLine();
      }
   }
}

编译并执行上述代码后,将产生以下结果 −

When the above code is compiled and executed, it produces the following result −

Max value is : 200

Recursive Method Call

方法可以自己调用自己。这称为 recursion 。以下是使用递归函数计算给定数字阶乘的示例——

A method can call itself. This is known as recursion. Following is an example that calculates factorial for a given number using a recursive function −

using System;

namespace CalculatorApplication {
   class NumberManipulator {
      public int factorial(int num) {
         /* local variable declaration */
         int result;
         if (num == 1) {
            return 1;
         } else {
            result = factorial(num - 1) * num;
            return result;
         }
      }
      static void Main(string[] args) {
         NumberManipulator n = new NumberManipulator();
         //calling the factorial method {0}", n.factorial(6));
         Console.WriteLine("Factorial of 7 is : {0}", n.factorial(7));
         Console.WriteLine("Factorial of 8 is : {0}", n.factorial(8));
         Console.ReadLine();
      }
   }
}

编译并执行上述代码后,将产生以下结果 −

When the above code is compiled and executed, it produces the following result −

Factorial of 6 is: 720
Factorial of 7 is: 5040
Factorial of 8 is: 40320

Passing Parameters to a Method

当带参数的方法被调用时,需要将参数传递到方法中。有三种方式可以将参数传递到方法中 -

When method with parameters is called, you need to pass the parameters to the method. There are three ways that parameters can be passed to a method −

Sr.No.

Mechanism & Description

1

Value parametersThis method copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument.

2

Reference parametersThis method copies the reference to the memory location of an argument into the formal parameter. This means that changes made to the parameter affect the argument.

3

Output parametersThis method helps in returning more than one value.