C# 中的函数与方法

在C#中,函数和方法都是一段可重用的代码块,用于实现特定的功能。函数是C#中的基本代码块之一,用于完成特定的任务和返回一个值。函数可以具有零个或多个参数,并且可以使用关键字来指定函数的访问级别和返回类型。函数可以在其他代码块中调用和重用,并且可以实现通用的算法。方法是C#中的另一个基本代码块,它定义在类中,并与类的对象相关联。方法可以访问类的状态和数据,并使用关键字指定访问级别和返回类型。方法可以传递参数,并具有访问权限控制。

函数和方法都是C#中可重用的代码块,它们各自具有不同的特性和用途:函数可以被直接调用和重用,而方法需要使用类的对象来调用和操作。在编写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();
        }
    }
}
posted @ 2020-06-18 12:46  lyshark  阅读(1264)  评论(0编辑  收藏  举报

loading... | loading...
博客园 - 开发者的网上家园