C#学习 方法篇

一、方法的定义和调用

定义一个打印方法,在主方法中调用该方法:

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

namespace 方法_定义和调用
{
    class Program
    {
        static void Main(string[] args)//主方法
        {
            Print();//调用打印方法
        }

        //定义打印方法
        static void Print()
        {
            Console.Write("===============打印方法===============");
        }
    }
}

控制台输出:

方法的定义和调用

二、方法的调用和参数传递

在程序中,除了主方法外,还定义了三个方法,分别是:合并数组方法(ArrMerge)、数组排序方法(ArrBubbleSort)和打印数组方法(PrintArr)。这三个方法都定义了整型数组的参数,只是形参的个数有所不同,此外,ArrMerge方法和ArrBubbleSort方法都有返回值,其类型为int型一维数组,而PrintArr没有返回值。

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

namespace 方法调用和参数传递
{
    class Program
    {
        //把两个数合并成一个数组,并对合并的数组排序
        //数组1:{3,5,7,2,1}
        //数组2:{9,12,4}
        static void Main(string[] args)
        {
            int[] arr1 = new int[] { 3, 5, 7, 2, 1 };//数组1
            int[] arr2 = new int[] { 9, 12, 4 };//数组2
            int arr1_len = arr1.Length;//数组1长度
            int arr2_len = arr2.Length;//数组2长度
            int[] arr3 = new int[arr1_len + arr2_len];//定义数组3(合并数组)

            arr3 = ArrMerge(arr1, arr2);//合并数组arr1,arr2

            arr3 = ArrBubbleSort(arr3); //对arr3排序

            PrintArr(arr3);//打印arr3

        }

        //合并两个一维数组
        static int[] ArrMerge(int[] a1, int[] a2)
        {
            int[] a3 = new int[a1.Length + a2.Length];
            for (int i = 0; i < a3.Length; i++)
            {
                if (i < a1.Length)
                {
                    a3[i] = a1[i];//合并数组a1
                }
                else
                {
                    a3[i] = a2[i - a1.Length];//合并数组a2
                }
            }
            return a3;
        }

        //对数组排序(冒泡排序法)
        static int[] ArrBubbleSort(int[] a)
        {
            for (int j = 0; j < a.Length - 1; j++)//共a.Length-1次冒泡
            {
                //一次冒泡
                for (int i = 0; i < a.Length - 1 - j; i++)//每次比较a.Length-1-j次
                {
                    if (a[i] > a[i + 1])
                    {
                        int temp = a[i];
                        a[i] = a[i + 1];
                        a[i + 1] = temp;
                    }
                }
            }
            return a;//返回数组a
        }

        //打印数组
        static void PrintArr(int[] a)
        {
            for (int i = 0; i < a.Length; i++)
            {
                Console.Write(a[i] + " ");
            }
        }
    }
}

控制台输出:

方法调用和参数传递

三、方法重载

这里选择重载求数组最大值Max方法作为方法重载的例子,方法重载后,参数类型或者参数个数和返回值类型应该改变。

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

namespace 方法重载
{
    class Program
    {
        //求int型数组最大值
        static int Max(int[] a)
        {
            int max = a[0];//定义最大值
            for (int i = 1; i < a.Length; i++)
            {
                max = max > a[i] ? max : a[i];//使用三目运算符?:来比较相邻元素,得到最大值并重新赋值给max
            }
            return max;
        }

        //求float型数组最大值(重载Max方法)
        static float Max(float[] a)
        {
            float max = a[0];
            for (int i = 1; i < a.Length; i++)
            {
                max = max > a[i] ? max : a[i];
            }
            return max;
        }

        static void Main(string[] args)
        {
            int[] a1 = { 1, 2, 3, 6, 2, 5 };
            float[] a2 = { 1.1f, 5.2f, 3f, 0.2f };
            Console.WriteLine(Max(a1));//调用int型Max方法
            Console.WriteLine(Max(a2));//调用float型Max方法
        }
    }
}

控制台输出:

方法重载

四、方法中参数修饰符ref和out

ref:引用参数,输出前变量必须赋值
out:输出参数,返回前必须在方法中赋值,用在函数可能返回多个值的时候

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

namespace 参数修饰符_ref和out
{
    class Program
    {
        //ref:引用参数,传递前变量必须赋值
        //out:输出参数,返回前在方法中必须赋值,用在函数可能返回多个值的时候
        static void Main(string[] args)
        {
            int a = 5;
            int b;
            Change(ref a);//调用Change方法
            Change1(out b);//调用Change1方法
            Console.WriteLine(a);
            Console.WriteLine(b);
        }

        //含有ref引用参数的Change方法
        static void Change(ref int a)
        {
            a = 10;
        }

        //含有out输出参数的Change1方法
        static void Change1(out int a)
        {
            a = 100;
        }
    }
}

控制台输出:

参数修饰符ref和out

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

斗乐八神

你的鼓励将是我最大的支持~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值