c#中的特殊的方法总结

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/bad_boy627056049/article/details/78785139

1、ref参数方法

ref用于修饰方法的参数,表示该参数为引用传递,而不是值传递,例如:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Fuction
{
    class Program
    {
        static void Main(string[] args)
        {
            int testParameter;
            testParameter = 10;
            RefTest(ref testParameter);
            Console.WriteLine(testParameter);
            Console.ReadKey();
        }

        private static void RefTest(ref int parameter)
        {
            parameter = parameter + 1;
        }
    }
}
需要注意的地方有一下几点:
  1. ref参数类型的方法在定义与调用的时候都需要加ref
  2. ref参数在调用前必须初始化,这个与out参数不同,后者不需要初始化,这也是两者的区别 
  3. 希望在调用函数时候修改被传入的参数时候可以使用ref类型的参数

2、out参数方法

out参数与ref参数一样也是引用传递,不是值传递,调用方法也一样,需要在定义方法与调用方法时候都加上out修饰符,但是它们之间的区别out参数在调用前不必初始化,它可以在方法中初始化,在方法调用结束后更新它的值,用于在需要返回多个返回值的应用场合,用out参数作为一个返回值,return返回一个值。例程如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Fuction
{
    class Program
    {
        static void Main(string[] args)
        {
            int testParameter;
            testParameter = 10;
            int a = 10, b = 20, c, d;
            RefTest(ref testParameter);
            c = OutTest(a, b, out d);

            Console.WriteLine(testParameter);
            Console.WriteLine($"a+b={c},b-a={d}");
            Console.ReadKey();
        }

        private static void RefTest(ref int parameter)
        {
            parameter = parameter + 1;
        }

        private static int OutTest(int a, int b, out int d)
        {
            d = b - a;
            return a + b;
        }
    }
}

3、params可变参数

params表示可以变的参数,方法中只允许一个该参数,且必须是最后一个参数,使用方法如实例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Fuction
{
    class Program
    {
        static void Main(string[] args)
        {
            int testParameter;
            testParameter = 10;
            int a = 10, b = 20, c, d;
            RefTest(ref testParameter);
            c = OutTest(a, b, out d);
            int m1 = 5, m2 = 6, m3 = 7, m4 = 8;
            Console.WriteLine(testParameter);
            Console.WriteLine($"a+b={c},b-a={d}");

            ParamsTest(a, b, m1, m2, m3, m4);
            Console.ReadKey();
        }

        private static void RefTest(ref int parameter)
        {
            parameter = parameter + 1;
        }

        private static int OutTest(int a, int b, out int d)
        {
            d = b - a;
            return a + b;
        }

        private static void ParamsTest(int a, int b, params int[] inter)
        {
            int len = inter.Length;
            Console.WriteLine($"a的值为{a},b的值为{b},数组的程度为{len}");
        }
    }
}

4、可以选参数

可以选参数是指该参数在调用时候可以传入,也可以不用传入,不传入则该参数会自动使用默认值,可以选参数遵循的原则如下:

  1. 方法定义时,如果某个参数为可选参数,必须指定默认值
  2. 可选参数的默认值必须是常数或者常数表达式
  3. 可选参数后面的参数必须也是可选的,即可选参数必须在最后

实例与后面的部分一同给出

5、命名参数

一般多个参数的方法,传入参数时候形参与实参的对应是通过参数顺兴一一对应的,而如果通过命名参数传入参数,就不用关心参数的顺序了,因为通过指定形参进行与实参对应,参数:传入的值

直接看例程:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Fuction
{
    class Program
    {
        static void Main(string[] args)
        {
            int testParameter;
            testParameter = 10;
            int a = 10, b = 20, c, d;
            RefTest(ref testParameter);
            c = OutTest(a, b, out d);
            int m1 = 5, m2 = 6, m3 = 7, m4 = 8;
            Console.WriteLine(testParameter);
            Console.WriteLine($"a+b={c},b-a={d}");

            ParamsTest(a, b, m1, m2, m3, m4);
            KeXuanCanshu(a, true);//传入参数为true
            KeXuanCanshu(a);//不传入参数,默认为false
            MingMingCanshu(age: 18, name: "Jack");//顺序与定义时候相反
            Console.ReadKey();
        }

        private static void RefTest(ref int parameter)
        {
            parameter = parameter + 1;
        }

        private static int OutTest(int a, int b, out int d)
        {
            d = b - a;
            return a + b;
        }

        private static void ParamsTest(int a, int b, params int[] inter)
        {
            int len = inter.Length;
            Console.WriteLine($"a的值为{a},b的值为{b},数组的程度为{len}");
        }

        private static void KeXuanCanshu(int a, bool b = false)
        {
            Console.WriteLine($"a={a},b={b},b默认为false");
        }

        private static void MingMingCanshu(string name, int age)
        {
            Console.WriteLine($"姓名为{name},年龄为{age}");
        }
    }
}






猜你喜欢

转载自blog.csdn.net/bad_boy627056049/article/details/78785139