.Net基础07

2017-05-16  本文已影响13人  Nicole__Zhang

方法的调用

namespace _01方法的调用
{
    class Program
    {
        //字段 属于类的字段 (C#中没有全局变量这一说)
        public static int _number = 10;//使用静态变量模拟 全局变量
        static void Main(string[] args)
        {
            int a = 3;
            int res = Test(a);//传参 a是实参
            Console.WriteLine(res);
            Console.WriteLine(a); //输出 3
            Console.WriteLine(_number);  //输出 10
            Console.ReadKey();
        }
        public static int Test(int a)  //a是形参
        {
            Console.WriteLine(_number); //输出 10
            a = a + 5;
            return a;
        }
        public static void TestTwo() //该方法未调用
        {
            Console.WriteLine(_number); //说明_number 可以被访问到
        }
    }
}
结果.png

我们在Main()函数中,调用Test()函数,我们管Main()函数称之为调用者,管Test()函数称之为被调用者。

static void Main(string[] args)
{
    //写一个方法 判断一个年份是否是闰年
    bool result = isRunNian(Convert.ToInt32(Console.ReadLine()));
    Console.WriteLine(result);
    Console.ReadKey();
}

/// <summary>
/// 判断给定的年份是否是闰年
/// </summary>
/// <param name="year">年份</param>
/// <returns>结果</returns>
public static bool isRunNian(int year)
{
    bool b = (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
    return b;
}
结果.png

out参数

如果你在一个方法中,返回多个相同类型的值的时候,可以考虑返回一个数组
但是,如果返回多个不同类型的值的时候,返回数组就不行了,那么这个时候,
我们可以考虑使用out参数。
out参数就侧重于在一个方法中可以返回多个不同类型的值。

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

namespace _03out参数
{
    class Program
    {
        static void Main(string[] args)
        {
            //写一个方法 求一个整数数组中的 最大值最小值 总和 平均值
            int[] numbers = {1,3,4,5,74,43,23,21,5};

            int[] res = GetMaxMinSumAvg(numbers);
            Console.WriteLine("最大值{0},最小值{1},总和{2},平均值{3}", res[0], res[1], res[2], res[3]);

            int max = 0;
            int min = 0;
            int sum = 0;
            int avg = 0;
            Test(numbers, out max, out min, out sum, out avg);
            Console.WriteLine("使用了out 参数 最大值{0},最小值{1},总和{2},平均值{3}", max, min, sum, avg);

            Console.ReadKey();
        }

        /// <summary>
        /// 求一个 数组中的 最大值最小值 总和 平均值
        /// </summary>
        /// <param name="nums">最大值最小值 总和 平均值 一次排列的数组</param>
        /// <returns>结果数组</returns>
        public static int[] GetMaxMinSumAvg(int[] nums)
        {
            int[] result = new int[4];
            //假设 res[0] 最大值 res[1] 最小值 res[2] 总和 res[3] 平局值
            result[0] = nums[0];
            result[1] = nums[0];
            result[2] = 0;

            for (int i = 0; i < nums.Length; i++)
            {
                if (nums[i] > result[0])
                {
                    result[0] = nums[i];
                }
                if (nums[i] < result[1])
                {
                    result[1] = nums[i];
                }
                result[2] += nums[i];
            }
            result[3] = result[2] / nums.Length;

            return result;    
        }

        /// <summary>
        /// 求一个整数数组中的 最大值最小值 总和 平均值
        /// </summary>
        /// <param name="nums">要求值的数组</param>
        /// <param name="max">多余返回的最大值</param>
        /// <param name="min">多余返回的最小值</param>
        /// <param name="sum">多余返回的总和</param>
        /// <param name="avg">多余返回的平均值</param>
        public static void Test(int[] nums, out int max, out int min, out int sum, out int avg)
        {
            //out 参数要求在方法的内部必须为其赋值
            max = nums[0];
            min = nums[0];
            sum = 0;

            for (int i = 0; i < nums.Length; i++)
            {
                if (nums[i] > max)
                {
                    max = nums[i];
                }
                if (nums[i] < min)
                {
                    min = nums[i];
                }
                sum += nums[i];
            }
            avg = sum / nums.Length;
        }
    }
}
结果.png

ref参数

能够将一个变量带入一个方法中进行改变,改变完成后,再讲改变后的值带出方法。
ref参数要求在方法外必须为其赋值,而方法内可以不赋值。

例子.png 运行结果.png
class Program
{
    static void Main(string[] args)
    {
        //使用方法来交换两个 int类型的变量
        int a = 10;
        int b = 30;
        ExChange(ref a, ref b);

        Console.WriteLine("a = {0}, b = {1}",a ,b);
        Console.ReadKey();
        
    }
    public static void ExChange(ref int a, ref int b)
    {
        int temp = a;
        a = b;
        b = temp;
    }
}
结果.png

params可变参数

将实参列表中跟可变参数数组类型一致的元素都当做数组的元素去处理。
params可变参数必须是形参列表中的最后一个元素。

例子.png

方法的重载

概念:方法的重载指的就是方法的名称相同给,但是参数不同
参数不同,分为两种情况
1)、如果参数的个数相同,那么参数的类型就不能相同。
2)、如果参数的类型相同,那么参数的个数就不能相同。
方法的重载跟返回值没有关系。

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(M(10, 20));
        Console.WriteLine(M(10, 20, 30));
        Console.WriteLine(M(2.0, 4.6));
        Console.WriteLine(M("WQE", "DF"));

        Console.ReadKey();
    }
    public static int M(int n1, int n2)
    {
        return n1 + n2;
    }
    public static int M(int n1, int n2, int n3)
    {
        return n1 + n2 + n3;
    }
    public static double M(double n1, double n2)
    {
        return n1 + n2;
    }
    public static string M(string s1, string s2)
    {
        return s1 + s2;
    }
结果.png

往期回顾

图文无关.png
上一篇 下一篇

猜你喜欢

热点阅读