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();
}
}
}
C# 中的函数与方法的更多相关文章
- vlookup函数基本使用--如何将两个Excel表中的数据匹配;excel表中vlookup函数使用方法将一表引到另一表
vlookup函数基本使用--如何将两个Excel表中的数据匹配:excel表中vlookup函数使用方法将一表引到另一表 一.将几个学生的籍贯匹配出来‘ 二.使用查找与引用函数 vlookup 三. ...
- 查看dll中的函数(方法)
https://jingyan.baidu.com/article/5553fa82b953b365a23934b7.html 查看dll中的函数(方法) 听语音 | 浏览:2004 | 更新:201 ...
- shell 从函数文件中调用函数的方法
你可以把所有的函数存储在一个函数文件中 你可以把所有的文件函数加载到当前脚本或命令行 加载函数文件中所有函数的方法: source xxx.sh
- MySQL中count函数使用方法详解
count函数是用来统计表中或数组中记录的一个函数,下面我来介绍在MySQL中count函数用法与性能比较吧. count(*) 它返回检索行的数目, 不论其是否包含 NULL值. SELECT ...
- odoo继承父类中的函数(方法)
使用_inherit继承父类重新设计新类时,可以调用父类中的函数,具体为: 第一步:获得某个模型('model.name')的数据集并进行某种集合操作(model_function),从而获得想要的数 ...
- 【转载】php中iconv函数使用方法
原文:http://www.phpweblog.net/star65225692/archive/2011/03/23/7524.html 在选择用什么工具开发,唯一的指导标准就是:用最少的人 ...
- php中iconv函数使用方法
最近在做一个程序,需要用到iconv函数把抓取来过的utf-8编码的页面转成gb2312, 发现只有用iconv函数把抓取过来的数据一转码数据就会无缘无故的少一些. iconv函数库能够完成各种字符集 ...
- 浅谈MFC类CrackMe中消息处理函数查找方法
最近一个学姐发给我了一份CrackMe希望我解一下,其中涉及到了MFC的消息函数查找的问题,就顺便以此为例谈一下自己使用的消息函数查找的方法.本人萌新,如果有任何错漏与解释不清的地方,欢迎各路大佬指正 ...
- javascript中eval()函数使用方法
本教程主要重介绍eval()函数的语法与使用方法,及在后面我还补充了eval()解析json数据的相关例子,希望文章能帮助到各位深入理解eval()使用方法吧. 前几天说到js中尽量不要使用eva ...
- fmt 包中的函数和方法
/ Fprintf 将参数列表 a 填写到格式字符串 format 的占位符中// 并将填写后的结果写入 w 中,返回写入的字节数func Fprintf(w io.Writer, format st ...
随机推荐
- VS Code 2022路线图:大量Spring Boot优化提上日程
1月20日,一名微软开发者发布了一篇标题为<Java on Visual Studio Code Update>的文章. 文中介绍了VS Code 2021年的亮点,同时还透露了VS Co ...
- vue prop 会接收不同的数据类型
refAge: { type: Number, default: 0 }, refName: { type: String, default: '' }, hotDataLoading: { type ...
- 使用 FormatMessage 格式化 Windows 错误码.md
https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-formatmessage #include <str ...
- NLP复习之神经网络
NLP复习之神经网络 前言 tips: 设计神经网络时,输入层与输出层节点数往往固定,中间层可以自由指定: 神经网络中的拓扑与箭头代表预测过程数据流向,与训练的数据流有一定区别: 我们不妨重点关注连接 ...
- 【JavaWeb】JSP基础和应用
JSP基础 JSP简介 JSP全称是Java Server Page,它和Servlet一样,也是sun公司推出的一套开发动态web资源的技术,称为JSP/Servlet规范.JSP的本质其实就是一个 ...
- 【rt-thread】移植touchgfx时出现如下错误和现象
[问题描述] 基于cubemx生成的touchgfx工程,移植入rt-threadkeil编译报重复定义 加载到文件组中的文件奇妙的出现了 Src_ .Device_.i2c_.Keil_ 前缀,这 ...
- [转帖]/dev/random 和 /dev/urandom的一点备忘
https://www.cnblogs.com/ohmygirl/p/random.html 1. 基本介绍 /dev/random和/dev/urandom是Linux系统中提供的随机伪设备,这两 ...
- [转帖]Prometheus 监控之 Blackbox_exporter黑盒监测 [icmp、tcp、http(get\post)、dns、ssl证书过期时间]
Blackbox_exporter 主动监测主机与服务状态 Prometheus 官方提供的 exporter 之一,可以提供 http.dns.tcp.icmp 的监控数据采集 官方github: ...
- [转帖]prometheus node-exporter 全部指标说明
https://www.cnblogs.com/276815076/p/16383615.html Basic CPU / Mem / Disk Info Basic CPU / Mem / Disk ...
- OpenGauss 单机版安装
OpenGauss 单机版安装 银河麒麟的前置事项 yum -y install libaio-devel flex bison ncurses-devel glibc-devel patch rea ...