当前位置:网站首页>C#入门系列(九) -- 方法使用
C#入门系列(九) -- 方法使用
2022-06-09 09:18:00 【InfoQ】
方法调用
1.1 定义方法的调用
//调用属于同一个类的有形参方法
方法名(实参列表)
//调用属于同一个类的无形参方法方法所属的类名
方法名()
//调用属于不同类的有形参方法方法所属的类名
方法名(实参列表)
//调用属于不同类的无形参方法
方法名()
1.2 命名参数
- 方法的声明与使用位置参数的方法声明完全一样;
- 进行方法调用时,形参的名字后必须跟着冒号和实际的参数值或表达式,
方法名(形参名:参数值,...,形参名:参数值)
class Program
{
static void Main(string[] args)
{
// 位置参数
Sum(1, 2, 3);
// 命名参数
Sum(b: 1, c: 2, a: 3);
// 混合使用
Sum(1, b: 3, c: 2);
Console.ReadLine();
}
static void Sum(int a,int b,int c)
{
Console.WriteLine("a={0},b={1},c={2}", a, b, c);
}
}
方法递归
- 递归调用:方法直接或间接的调用自己,称为递归调用。
- 递归会产生很优雅的代码。
- C#允许方法实现直接递归调用和间接递归调用。
class Program
{
static void Main(string[] args)
{
int i = 10;
// 结束条件,找一个比较大的数。 后续算法测试通过,可变为true
while (i < 100)
{
if (Recursion(i) > 100)
{
Console.WriteLine("第{0}个月兔子达成100对", i);
break;
}
i++;
}
Console.ReadLine();
}
static int Recursion(int n)
{
// 递归出口:兔子第一个月第二个月都为1对
if (n == 1 || n == 2)
{
return 1;
}
else
{
// 递归条件:兔子第三月起是前两个月的兔子之和
return Recursion(n - 1) + Recursion(n - 2);
}
}
}
- 通常可以将一个比较大的问题层层转化为一个与原问题相类似的、规模较小的问题进行求解,最终实现对原问题的求解。
- 使用递归时一定要注意必须具备让递归结束的条件。
方法重载
- 定义:C#的一个类中超过一个方法具有相同名称的现象叫做方法重载。
- 在C#中,实现方法重载时必须保证使用相同名称的每个方法有一个和其他方法不同的签名,并且判断方法重载是否合法时编译器只考查这个方面。
- C#重载方法的条件:方法名相同,方法的参数类型或参数个数不同。
static void Sum(int a, int b) { }
static void Sum(int x, int y) { }
static void Sum(int a, int b) { }
static void Sum(string a, int b) { }
static void Sum(int a, int b, int c) { }
边栏推荐
- [code comment] Doxygen
- GLCC 首届编程夏令营|欢迎报名 Layotto、KusionStack、Nydus、Kata Containers!
- Write InputStream to file write multipartfile to file
- MySQL basic query statement
- Paper understanding [RL - exp replay] - an equivalence between loss functions and non uniform sampling in exp replay
- 倒计时 3 天 | SOFAChannel#28 SOFAArk 类隔离框架设计
- LeetCode_ Backtracking_ Difficulties_ 301. delete invalid brackets
- Sword finger offer09-- implement queue with two stacks
- Project interview questions
- Judge whether it is JSON or file stream
猜你喜欢

- Bean method ‘redisConnectionFactory‘ in ‘JedisConnectionConfiguration‘ not loaded because @Conditi

Linux在线安装一个Neo4j图数据库

Attachment 17: limited articles on network program interpretation

ERP 系统,编译和学习

neo4j实现社交推荐(四)

【图机器学习】图神经网络入门(一)谱图理论

Neo4j realizes social recommendation (4)

使用Canvas画出多个多边形Polygon

【科技、商业和管理】看剧学创业:《硅谷》第六季第1-2集

Error deleting environment variable path
随机推荐
HAVE FUN | SOFAArk 源码解析活动
Neo4j error when accessing the browser: serviceunavailable: websocket connection failure Due to security constraints in your
SSM详解
[code comment] Doxygen
判断是Json还是文件流
Project interview questions
[redis learning 11] data persistence of distributed cache, master-slave cluster
Paper understanding [RL - exp replay] - an equivalence between loss functions and non uniform sampling in exp replay
MySQL basic addition, deletion, modification and query exercise
环境变量Path误删除
[5 machine learning] the most understandable decision tree in the whole network (with source code attached)
three. JS learning notes (16) -- turbulent ocean
如何看待 Dapr、Layotto 这种多运行时架构?
ERP 系统,编译和学习
中位数图(前缀和)
LeetCode_回溯_困难_301. 删除无效的括号
了解图数据库neo4j(二)
XML to map (recursively call to read all node contents of XML) readxml read XML
MySQL basic query statement
Basic pointer ~ guide you to the introduction pointer