.Net基礎07

方法的調用

namespace _01方法的調用
{
    class Program
    {
        //字段 屬于類的字段 (C#中沒有全局變量這一說)
        public static int _number = 10;//使用靜態(tài)變量模擬 全局變量
        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()函數(shù)中,調用Test()函數(shù),我們管Main()函數(shù)稱之為調用者,管Test()函數(shù)稱之為被調用者。

  • 如果被調用者想要得到調用者的值:
    1)、傳遞參數(shù)。
    2)、使用靜態(tài)字段來模擬全局變量。

  • 如果調用者想要得到被調用者的值:
    1)、返回值

  • 不管是實參還是形參,都是在內存中開辟了空間的。

  • 方法的功能一定要單一。
    GetMax(int n1,int n2)
    方法中最忌諱的就是出現(xiàn)提示用戶輸入的字眼。

  • 小練習

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參數(shù)

如果你在一個方法中,返回多個相同類型的值的時候,可以考慮返回一個數(shù)組。
但是,如果返回多個不同類型的值的時候,返回數(shù)組就不行了,那么這個時候,
我們可以考慮使用out參數(shù)。
out參數(shù)就側重于在一個方法中可以返回多個不同類型的值。

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

namespace _03out參數(shù)
{
    class Program
    {
        static void Main(string[] args)
        {
            //寫一個方法 求一個整數(shù)數(shù)組中的 最大值最小值 總和 平均值
            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 參數(shù) 最大值{0},最小值{1},總和{2},平均值{3}", max, min, sum, avg);

            Console.ReadKey();
        }

        /// <summary>
        /// 求一個 數(shù)組中的 最大值最小值 總和 平均值
        /// </summary>
        /// <param name="nums">最大值最小值 總和 平均值 一次排列的數(shù)組</param>
        /// <returns>結果數(shù)組</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>
        /// 求一個整數(shù)數(shù)組中的 最大值最小值 總和 平均值
        /// </summary>
        /// <param name="nums">要求值的數(shù)組</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 參數(shù)要求在方法的內部必須為其賦值
            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參數(shù)

能夠將一個變量帶入一個方法中進行改變,改變完成后,再講改變后的值帶出方法。
ref參數(shù)要求在方法外必須為其賦值,而方法內可以不賦值。

例子.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可變參數(shù)

將實參列表中跟可變參數(shù)數(shù)組類型一致的元素都當做數(shù)組的元素去處理。
params可變參數(shù)必須是形參列表中的最后一個元素。

例子.png

方法的重載

概念:方法的重載指的就是方法的名稱相同給,但是參數(shù)不同。
參數(shù)不同,分為兩種情況
1)、如果參數(shù)的個數(shù)相同,那么參數(shù)的類型就不能相同。
2)、如果參數(shù)的類型相同,那么參數(shù)的個數(shù)就不能相同。
方法的重載跟返回值沒有關系。

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
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容