C# 中的函数与方法
2023-02-18 16:46:25 时间
简单的函数定义:
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();
}
}
}
相关文章
- Linux Shell 编程基础详解——吐血整理,墙裂推荐!
- Linux nsenter 命令简介及 切换宿主机网络为docker容器网络实践
- Linux下安装MySQL及远程连接MySQL
- linux nc命令
- Git安装及使用以及连接GitHub方法详解
- Linux curl命令详解
- 测试必会之 Linux 三剑客之 sed
- Linux常用统计命令之wc
- Linux常用统计命令之uniq
- Linux常用统计命令之sort
- linux命令总结dd命令详解
- 测试必会之 Linux 三剑客之 awk
- 测试必会之 Linux 三剑客之 grep
- linux文件句柄数
- Linux常用命令
- 浅谈Linux
- Git的简单的基本使用
- Android开发——导入github安卓项目源码
- 前端学习 linux —— 软件安装(Ubuntu)
- 前端学习 linux —— shell 编程