当前位置:网站首页>具名参数
具名参数
2023-01-19 15:33:03【阿斯提尼】
当我们在调用参数很多的且具有可选参数的方法时就可以用到具名参数,比如:
有一个方法
public class ParameterClass
{
public void TestFunc(int a, bool b = false, string c = null, int d = 0, int e = 0)
{
if (b)
{
Console.WriteLine("第二个参数b = " + b);
}
if (!string.IsNullOrEmpty(c))
{
Console.WriteLine("第三个参数c = " + c);
}
if (d != 0)
{
Console.WriteLine("第四个参数d = " + d);
}
if (e != 0)
{
Console.WriteLine("第五个参数e = " + e);
}
Console.ReadKey();
}
}
正常情况下我们如果想给e赋值,而其它参数采用默认值时我们一般将所有的参数都列出来,这种操作在参数很多的情况下很费时且很容易出错(例子只举了5个参数,而实际我们有可能会遇到10个、20个等参数的情况)
new ParameterClass().TestFunc(0, false, null, 0, 666666);
而使用具名参数就可以很轻松地给特定的参数赋值,简单还不容易出错,此时采用具名参数我们就可以这样做:
new ParameterClass().TestFunc(0, c: "HelloWorld", e: 666666);
得到的结果如下:
边栏推荐
猜你喜欢
随机推荐
- 负电压是怎么产生的原理分析
- 迟滞比较器Hysteresiswindow和comparator(窗口比较器)原理
- 残留物与电子PCBA 的可靠性和三防漆涂敷前后可能导致电路板出现故障的变量
- 京东探索研究院 | 2023年十大科技趋势
- A variety of Chinese dialect voice landing applications, Microsoft's intelligent voice unlocks more interactive scenarios
- Is Alipay's annuity insurance worth buying?is it safe?
- Which is the top ten insurance for annuity insurance? Is it safe?
- Use MeterSphere beanshell global assertion reference JSONObject solution
- CNN+LSTM+Attention实现时间序列预测(PyTorch版)
- WebView加载heml代码简单应用
- CTA-Sensitive Behavior-AppOps Solution
- Beyond TensorFlow?Yann LeCun: "Why? PyTorch. That's why."
- 蜻蜓安全工作台程序编排简要说明
- WSL2安装systemd方法
- 安信证券开户安全吗?佣金是万几?
- 大智慧在上面开户安全吗?谁能告诉我一下
- 从合并石子学区间DP
- Golang的基本数据类型-基本使用
- 线扫相机DALSA--卡间同步
- 海康visionmaster-图像Bitmap和CmvdImage互转的方法
- 【threejs】根据点绘制直线
- 【threejs】threesjs 初学场景构建
- 第十三章 UML建模
- 为什么很多年轻人,都被 “伪自律” 给拖垮了?
- 小黑年前实习倒数第二天,又到了开心的疯狂星期四的leetcode之旅:剑指 Offer 27. 二叉树的镜像&&剑指 Offer 28. 对称的二叉树
- Servlet —— Servlet API
- 使用nvm管理node版本(window版)
- typescript学习笔记(三)
- vtkPolyData数据的空间变换
- 深入透析 类的 访问修饰符 public、 protected 、private
- C语言基础 — ( 函数——模块化设计)
- Wider Face+YOLOV8人脸检测
- Wider Face+YOLOV7人脸检测
- 【Android安全】Google Hardware-backed Keystore | SafetyNet | 远程证明Remote Attestation
- [转]SteamVR 1.x️一、实现手与物体交互——基于[CameraRig]
- PHP MySQL Where 子句
- PHP MySQL Order By 关键词
- 我理解的卓越工程
- 各种huggingface分词器对比
- PHP MySQL Order By keywords




