C#方法的参数

 C#中方法的参数有四种类型

        1. 值参数         (不加任何修饰符,是默认的类型)
   2. 引用型参数  (ref 修饰符声明)
   3. 输出参数    (out 修饰符声明)
   4. 数组型参数  (params 修饰符声明)


1.值参数

 值类型是方法默认的参数类型,采用的是值拷贝的方式。也就是说,如果使用的是值类型,则可以在方法中更改该值,
 
但当回调方法时,不会保留更改的值。(值类型有简单类型,结构类型,枚举类型)。

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

namespace lession7
{
    class MyClass
    {
        public void Swap(int x, int y)
        {
            int k;
            k = x;
            x = y;
            y = k;
            Console.WriteLine("在MyClass中x={0},y={1}", x, y);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            int x = 12, y = 45;
            Console.WriteLine("调用Swap函数前x={0},y={1}", x, y);
            MyClass my = new MyClass();
            my.Swap(x,y);
            Console.WriteLine("调用Swap函数后x={0},y={1}",x,y);
            Console.ReadLine();
        }
    }
}
结果:

调用Swap函数前x=12,y=45
在MyClass中x=45,y=12
调用Swap函数后x=12,y=45

2.引用参数

ref关键字使参数按引用传递,若要使用 ref 参数,必须将参数作为 ref 参数显式传递到方法,ref 参数的值被传递到 ref 参数。 
其效果是,
当控制权传递回调用方法时,在方法中对参数所做的任何更改都将反映在该变量中。(引用类型有:类类型,数组类型,接口类型,委托类型)
2.1. 若要使用 ref 参数,则方法定义和调用方法都必须显式使用 ref关键字。
2.2. 传递到 ref 参数的参数必须最先初始化。这与 out 不同,out 的参数在传递之前不需要显式初始化。
2.3. 如果一个方法采用 ref  out 参数,而另一个方法不采用这两类参数,则可以进行重载。

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

namespace lession7
{
    class MyClass
    {
        public void Swap( ref int x, ref int y)
        {
            int k;
            k = x;
            x = y;
            y = k;
            Console.WriteLine("在MyClass中x={0},y={1}", x, y);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            int x = 12, y = 45;
            Console.WriteLine("调用Swap函数前x={0},y={1}", x, y);
            MyClass my = new MyClass();
            my.Swap(ref x,ref y);
            Console.WriteLine("调用Swap函数后x={0},y={1}",x,y);
            Console.ReadLine();
        }
    }
}

结果:

调用Swap函数前x=12,y=45
在MyClass中x=45,y=12
调用Swap函数后x=45,y=12


3.

  输出类型(out类型)
out 关键字会导致参数通过引用来传递。这与 ref 关键字类似。
若要使用 out 参数,必须将参数作为 out 参数显式传递到方法,out 参数的值不会传递到 out 参数。

其效果是,当控制权传递回调用方法时,在方法中对参数所做的任何更改都将反映在该变量中

3.1. ref 要求变量必须在传递之前进行初始化,out 参数传递的变量不需要在传递之前进行初始化

3.2. 尽管作为out 参数传递的变量不需要在传递之前进行初始化,但需要在调用方法初始化以便在方法返回之前赋值。
out适合用在需要retrun多个返回值的地方,而ref则用在需要被调用的方法修改调用者的引用的时候。

当希望方法返回多个值时,声明 out 方法非常有用。使用 out 参数的方法仍然可以返回一个值。一个方法可以有一个以上的 out 参数。


using System;
using System.Collections.Generic;
using System.Text; 
class Program
    {
        static void Main(string[] args)
        {
         
            Student s = new Student("Tom",21);
            int nCurrentAge;
            s.Grow(3, out nCurrentAge);
            Console.WriteLine(s.nAge);
            Console.ReadLine();
        }
    }
   public  class Student
    {
        public string strName;
        public int nAge;
        public Student(string _strName, int _nAge)
        {
            strName = _strName;
            nAge = _nAge;
        }
        public void Grow(int _nSpan, out int _nOutCurrentAge)
        {
            nAge += _nSpan;
            _nOutCurrentAge = nAge;
        }
       
    }
}
结果:

24

4.   数组型参数类型(params类型)

params关键字可以指定在参数数目可变处采用参数的方法参数。也就是说。使用params可以自动把你传入的值按照规则转换为一个新建的数组。
在方法声明中的 params
关键字之后不允许任何其他参数,并且在方法声明中只允许一个 params关键字。

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

namespace lession7
{
    class Student
    {
        public string strName;
        public int nAge;
        public ArrayList  strArrHobby = new ArrayList();
        public Student(string _strName, int _nAge)
        {
            this.strName = _strName;
            this.nAge = _nAge;
        }
        public void SetHobby(params string[] _strArrHobby)
        {
            for (int i = 0; i < _strArrHobby.Length; i++)
            {
                this.strArrHobby.Add(_strArrHobby[i]);
            }
        }
    }
}

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

namespace lession7
{
    class Program
    {
        static void Main(string[] args)
        {
            Student s = new Student("Tom",21);
            int nCurrentAge;
            s.Grow(3, out nCurrentAge);
            Console.WriteLine(s.nAge);*/
            Student s = new Student("Tom", 21);
            s.SetHobby("游泳","篮球","足球");
            for (int i = 0; i < s.strArrHobby.Count; i++)
            {
                Console.WriteLine(s.strArrHobby[i]);
            }
            Console.ReadLine();
        }
    }
输出结果:

游泳

篮球

足球

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值