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 ...
随机推荐
- 最火前端Web组态软件(可视化)
友情提示:本文为原创文章,转载请注明出处,商务合作请私信!!! 前言: 随着物联网.大数据等技术高速发展,我们逐步向数字化.可视化的人工智能(AI)时代的方向不断迈进.智能时代是工业 4.0 时代 ...
- AtCoder Beginner Contest 172 (C题前缀和 + 二分,D题筛因子,E题容斥定理)
AB水题, C - Tsundoku 题目描述 有两摞书,一摞有 $n$ 本,从上至下每本需阅读 $a_i$ 分钟,一摞有 $m$ 本,从上至下每本需阅读 $b_i$ 分钟,问最多能在 $k$ 分钟内 ...
- 2018年蓝桥杯B组C/C++国赛题解
1.换零钞 x星球的钞票的面额只有:100元,5元,2元,1元,共4种. 小明去x星旅游,他手里只有2张100元的x星币,太不方便,恰好路过x星银行就去换零钱. 小明有点强迫症,他坚持要求200元换出 ...
- OKR之剑·实战篇05:OKR致胜法宝-氛围&业绩双轮驱动(上)
作者:vivo 互联网平台产品研发团队 本文是<OKR 之剑>系列之实战第 5 篇-- 我们的OKR执行如此顺利,离不开我们的"双轮驱动".类似于亚马逊的"飞 ...
- PVE API创建虚拟机
度娘,谷歌都搜了一圈没有找到通过PVE API创建虚拟机的方式, 于是查官网自己试了试,部分代码抄的Sam Liu大佬的作业,感谢大佬. python代码如下: import requests # s ...
- OpenSCA受邀出席2023 Open Compliance Summit
近日,由Linux基金会主办的2023 Open Compliance Summit(开放合规峰会,简称OCS)在日本东京隆重召开.悬镜安全旗下全球极客开源数字供应链安全社区OpenSCA受邀参与,O ...
- mysql备份恢复总结
mysqldump备份注:例子中的语句都是在mysql5.6下执行------------------基础------------------------一.修改my.cnf文件 vi /etc/my ...
- h5响应式方案设计
- python3使用json、pickle和sqlite3持久化存储字典对象
技术背景 在各种python的项目中,我们时常要持久化的在系统中存储各式各样的python的数据结构,常用的比如字典等.尤其是在云服务类型中的python项目中,要持久化或者临时的在缓存中储存一些用户 ...
- chorm如何静音特定网页
如图所示,右键想要静音的网页标签,选择将这个网页静音即可