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();
}
}
}
文章出处:https://www.cnblogs.com/LyShark/p/13156969.html
本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?