方法、方法的参数ref out params

一、方法 
      方法表示类所能执行的计算操作。方法的基本要素按定义时书写的顺序依次包括方法的返回类型、方法名、参数列表和方法的执行体。
       方法的声明中,只有参数列表可以为空,其它3项则不可以缺少。即使没有返回值,也要声明方法的返回类型为void。
        在方法的代码中,只要执行到return语句就返回,剩余的代码将被跳过。在对程序进行编译时,如果在无条件的return语句后仍有代码,编译器会发出警告:“检测到不可达的代码”。
         若方法的返回类型为void,则方法的执行代码中可以没有return 语句,也可以包含单独的return语句;否则,方法的执行代码必须包含return语句,而且在return关键字后必须跟一个类型与方法返回类型相同的表达式。


二、参数类型
        调用方法时,传递给方法的参数类型应该与方法定义的参数类型相同,或者是能够隐式转换为方法定义的参数类型。定义方法时声明的参数叫做形式参数(形参),而实际调用时传递给方法的参数叫做实际参数(实参)。
        C#的方法可以接受的参数类型共有4种:
        1、普通参数,无须特别定义,参数为值类型时进行值传递(这时也叫值参数),参数为引用类型时进行引用传递。
        2、引用型参数,以关键字ref定义。采用引用传递。
        3、输出型参数,以关键字out字义。采用引用传递,其实参数可以不进行初始化;
        4、数组型参数,以关键字params定义,根据实参形式选择引用传递或值传递,包含数量可变的参数。

在使用引用型与输出型参数时,关键字ref与out不仅要在方法定义时指明,而且在方法调用时同样需要写了。

using System;
using System.Collections.Generic;
using System.Text;

namespace capprefout
{
    
class Program
    
{
        
static void Main(string[] args)
        
{
            Console.WriteLine(
"ref");
            Console.WriteLine(
"请输入整数a:");
            
int a = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine(
"请输入整数b:");
            
int b = Convert.ToInt32(Console.ReadLine());
            SwapPlus(
ref a, ref b);
            Console.WriteLine(
"a:" + a.ToString());
            Console.WriteLine(
"b:" + b.ToString());
            Console.WriteLine(
"out");
            
int iPlus, iminus;
            Plus(
out iPlus,out iminus, a, b);
            Console.WriteLine(
"a+b =" + iPlus);
            Console.WriteLine(
"a-b =" + iminus);
            Console.ReadLine();
        }


        
public static void SwapPlus(ref int a, ref int b)
        
{
            
int tmp = a;
            a 
= b + 3;
            b 
= tmp + 5;
        }

        
public static void Plus(out int iPlus,out int iminus,int a,int b)
        
{
            iPlus 
= a + b;
            iminus 
= a - b;
        }

    }

}


要使一个方法能够接受不同数量的参数,就要用到数组型参数,数组型参数使用有严格的规定:
        1、方法中只允许定义一个数组型参数,而且该参数必须位于参数列表中的最后;
        2、数组型参数所定义的数组必须是一维数组。
        3、数组型参数不能同时既为引用型参数又为输出型参数。
using System;
using System.Collections.Generic;
using System.Text;

namespace cappParams
{
    
class Program
    
{
        
static void Main(string[] args)
        
{
            
int[] p = 356 };
            Sotr(p);
            Console.WriteLine(p[
0].ToString());
            Console.WriteLine(p[
1].ToString());
            Console.WriteLine(p[
2].ToString());
            Console.WriteLine(
"上面是引用传递方式-------下面是值传递方式");
            
int p1 = 3;
            
int p2 = 5;
            
int p3 = 6;
            Sotr(p1, p2, p3);
            Console.WriteLine(p1.ToString());
            Console.WriteLine(p2.ToString());
            Console.WriteLine(p3.ToString());
            Console.ReadKey();
        }

        
public  static void Swap(ref int a, ref int b)
        
{
            
int tmp = a;
            a 
= b;
            b 
= tmp;
        }


        
public static  void Sotr(params int[] array)
        
{
            
if (array.Length != 3)
                
return;
            
if (array[0< array[1])
                Swap(
ref array[0], ref array[1]);
            
if (array[1< array[2])
                Swap(
ref array[1], ref array[2]);
            
if (array[0< array[1])
                Swap(
ref array[0], ref array[1]);
        }

    }


}


输出结果为:
6
5
3
上面是引用传递方式
-------下面是值传递方式
3
5
6
结果不一样的

三、方法的标识与重载
下面这是错误的:
        public static void myTest(ref int a, ref int b) {
        }


        
public static void myTest(int a,int b){
        }
原文地址:https://www.cnblogs.com/myx/p/178542.html