C# 中的函数与方法

简单的函数定义:

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

namespace ConsoleApplication1
{
    class Program
    {
        // 定义一个方法,并提供参数传递
        public static int GetMax(int x, int y)
        {
            return x > y ? x : y;
        }
        // 定义一个判断闰年的方法
        public static bool IsRun(int year)
        {
            bool ret = (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
            return ret;
        }

        static void Main(string[] args)
        {
            int max = Program.GetMax(100, 200);
            Console.WriteLine("最大值: {0}", max);

            bool ret = Program.IsRun(2020);
            Console.WriteLine("闰年: {0}", ret);

            Console.ReadKey();
        }
    }
}

方法传递数组/字符串:

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

namespace ConsoleApplication1
{
    class Program
    {
        // 方法传递数组
        public static int GetSum(int[] Array)
        {
            int sum = 0;
            for (int x = 0; x < Array.Length; x++)
                sum += Array[x];

            return sum;
        }
        // 方法传递字符串
        public static string IsString(string str)
        {
            return str;
        }

        static void Main(string[] args)
        {
            int[] Array = new int[] { 1, 2, 3, 4, 5 };

            int ret_sum = Program.GetSum(Array);
            Console.WriteLine("相加结果: {0}", ret_sum);

            string ret_str = Program.IsString("hello lyshark");
            Console.WriteLine("字符串: {0}", ret_str);

            Console.ReadKey();
        }
    }
}

Out 方法返回多个参数: 类似与C++中的多指针传递,就是说可以一次性传出多个参数。

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

namespace ConsoleApplication1
{
    class Program
    {
        // 返回 max => 最大值 / main => 最小值
        public static void GetNum(int[] Array, out int Max, out int Min)
        {
            int max = int.MinValue;
            int min = int.MaxValue;

            for (int i = 0; i < Array.Length; i++)
            {
                if (Array[i] > max)
                    max = Array[i];

                if (Array[i] < min)
                    min = Array[i];
            }
            Max = max;
            Min = min;
        }

        static void Main(string[] args)
        {
            int[] Array = new int[] { 2,6,9,3,10 };
            int Max = 0;
            int Min = 0;

            GetNum(Array,out Max,out Min);

            Console.WriteLine("最大值: {0}", Max);
            Console.WriteLine("最小值: {0}", Min);

            Console.ReadKey();
        }
    }
}

Out 实现参数返回:

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

namespace ConsoleApplication1
{
    class Program
    {
        // 自己实现TryParse
        public static bool MyTryParse(string Str,out int result)
        {
            result = 0;
            try
            {
                result = Convert.ToInt32(Str);
                return true;
            }
            catch
            {
                return false;
            }
        }

        static void Main(string[] args)
        {
          int number = 0;
          bool sys_ret = int.TryParse("123", out number);
          Console.WriteLine("系统转换结果输出: {0} 状态: {1}", number,sys_ret);

          bool my_ret = Program.MyTryParse("456", out number);
          Console.WriteLine("My转换结果输出: {0} 状态:{1}", number,my_ret);

          Console.ReadKey();
        }
    }
}

Ref 变量指针交换:

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

namespace ConsoleApplication1
{
    class Program
    {
        // 变量交换,类似于指针传递
        public static void Exchange(ref int x,ref int y)
        {
            int tmp = x;
            x = y;
            y = tmp;
        }

        static void Main(string[] args)
        {
            int x = 100;
            int y = 200;
            Exchange(ref x, ref y);

            Console.Write("交换后: x = {0} y = {1}", x, y);

            Console.ReadKey();
        }
    }
}

params 传递可变参数:

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

namespace ConsoleApplication1
{
    class Program
    {
        // 指定可变参数
        public static void GetName(int Number,params string[] Str)
        {
            for (int x = 0; x < Str.Length; x++)
                Console.Write(Number + "  " + Str[x] + "  ");
        }

        static void Main(string[] args)
        {
            GetName(1001,"admin", "lyshark", "guest");

            Console.ReadKey();
        }
    }
}

实现方法重载

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

namespace ConsoleApplication1
{
    class Program
    {
        // 方法重载
        public static double Sum(double x,double y)
        {
            return x + y;
        }
        public static int Sum(int x, int y)
        {
            return x + y;
        }

        static void Main(string[] args)
        {
            Console.WriteLine("int => {0}", Sum(10, 20));
            Console.WriteLine("double => {0}", Sum(10.5, 20.5));

            Console.ReadKey();
        }
    }
}

版权声明: 本博客,文章与代码均为学习时整理的笔记,博客中除去明确标注有参考文献的文章,其他文章【均为原创】作品,转载请务必【添加出处】,您添加出处是我创作的动力!

警告:如果您恶意转载本人文章,则您的整站文章,将会变为我的原创作品,请相互尊重!
原文地址:https://www.cnblogs.com/LyShark/p/13156969.html