具名参数 和 可选参数 是 C# framework 4.0 出来的新特性。

一. 常规方法定义及调用

public void Demo1(string x, int y)
{
//do something...
} public void Main()
{
//调用
Demo1("similar", );
}

调用时,参数顺序(类型)必须与声明一致,且不可省略。

二. 可选参数的声明及调用

可选参数分为两种情况: 1. 部分参数可选;   2. 全部参数都是可选

//部分可选(x为必选,y为可选)
public void Demo2(string x, int y = )
{
//do something...
} public void Main()
{
//调用
Demo2("similar"); // y不传入实参时,y使用默认值5
Demo2("similar", ); // y传入实参,则使用实参10
}

注: 当参数为部分可选时, 可选参数  的声明必须定义在 不可选参数(命名参数)的后面(如上: y 的声明在 x 之后),不然会出现如下错误提示:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAArkAAABsCAIAAADhUD1WAAAN1UlEQVR4nO3du47bVh7Hcb7GYgE9BR8g1ThFijxAOqYIBD3EdCkGdGkgrzAC3LGawm0M39bABF5zdhJkk01GyXgbF0E83pwtKJLn+ufhiLqR3w/+QBySOrxI4PnxkNIkCgAAICzZ9wYAAICDRlYAAAASsgIAAJCQFQAAgISsAAAAJGQFAAAgISsAAAAJWQEAAEjICgAAQEJWAAAAkgGywu2z828qF1d9X9TjFQAAYB82zgpXF9+cP7tVqur+Y/v+22fn61cBAICDtmlWuH123uQDIwDcPju/uLiohw7MsYerC+1/7GEJ6YXaXG1QwmjOM87RzCeeAADQ26ZZ4epCG0u4ujCyQjNHCxTrRZpY4Z0lv/Cb5pXnz26tLfC9RN8qAADQ0zbHFZr/aS7s9cGEaq4wq2tulQHsuxnuS3g4AgCADWzteQWry7f6aT0rhGZ1zW2GDYxl3Jf4GgYAAJEG+B6E/3EAs2fWrvbNexDirK4Xrv9lPa9gvaR9fIGBBQAA+uP3FQAAgISsAAAAJGQFAAAgISsAAAAJWQEAAEjICgAAQEJWmIIiS9K83PdWYGBFlmTFvjcCwBTsISvkqfKe4cpcJcm63Pl56p++bfJ6y1wlqZJ64TJPk9Y2e+xqTe4ayjz1TN3X8dyceUQPJQTt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zRxb22E2qm78HqNTfdcZHojoc3TVmvcg3D32pcVfMsbOxgaksGRIitQFNVZE8wKobv4znSzL287Y7mLDa21GfZvOuxQO6GssMpT/70Gc/NCG3b/rMC4wpiRFSiK6qzpZQX7aj48PXThbnex3eMKGu2+Q6gdKSv4n7tUO8gKTUBhUGFcvFmBoijKW5ufc44jK/T5qmSRNQ/xXWbakECRnQafG4jZgPq1gXaC9yCM5xtsl5lzm2PAexDBjFKPdjjxq+907AlZgaKo+Nr8nHMUWSHU7QWmN8P+j/LS6GubLwtEBYXAlykC7QSzgrK/j2CuWluL/j2Iena7/faXGrqWd75qYT/VQVY4YmQFiqLia/NzzhFkhfivSqJl3/vQhltw/PSsQFEUFarnL68GOeccflaI/aokDNbdjVWecrhG5OU/rle/vdv7aYiiqAOv6WQF3JNxD4WgMC5X//rPz7/8frMiLlAUJdS77978OMg5h6wAHJ8/P9y9efvT85dXFEVRofruzY9//PHnIOccsgIAAJCQFQAAgISsAAAAJGQFAAAgGSArlHmaJEmS8EM9AACM0GDjCkVGWgAAYISGuwdRZEna4y8sAACAo0BWAAAAErICAACQDJcVyjwlLAAAMDrDfmey+koEjzgCADAejCsAAAAJzysAAAAJWQEAAEjICgAAQEJWAAAAEn7jGQAASPjbUQAAQMLfpAYAABKyAgAAkJAVAACAhKwAAAAkZAUAACAhKwAAAAlZAQAASMgKAABAQlYAAAASsgIAAJCQFQAAgISsAAAAJGQFAAAg+T/eKhtJ7IqFMQAAAABJRU5ErkJggg==" alt="" />

//全部可选(x,y 均为可选参数)
public void Demo3(string x = "demo", int y = )
{
//do something...
} public void Main()
{
//调用
Demo3(); // x,y不传入实参时,x,y使用默认值 "demo",5
Demo3("similar"); // y不传入实参时,y使用默认值5
Demo3("similar", ); // x,y都传入实参
}

注: a.  当参数全部都为可选时,参数的声明顺序可以随意定义,不分先后。

b.  参数声明定义可以无顺序,但调用时必须与声明时的一致。

上面的调用只写的3种,其实还有一种,就是 x 使用默认值,y 传入实参,即 :  Demo3(10);

但这样调用会报错,因为Demo3的第一个参数是 string 类型,错误消息如图:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAtUAAAEACAIAAADzyh09AAAgAElEQVR4nO2dvY7jSpqmdQdrpbWA7mA8Gm20MUAByjHGGH/H4xgDIZHXkN4YAtOrvIFarJEC0jgAjbQSp41FJdDVNQ0k0Gh19dTM7sxWZW/uMbat3TMLrhEk4++LYFCiKFF6Hrw4J4uigsEgxXj5xd+sAgAAABiO79/fOjU7dCYBAADgpMB/AAAAwNjgP06GMp9lxcb8Z14eMDsAAABhxvYfRVaJteKmqGazWv7nRSZv3zfx426KapZVm8CnVVVVZT6bGS7A+WeQTZGl7Wh/xXQfTSpYEAAAOEaOxX/UlCfoP1oPoP7luAQ54Z7+Q3AfbTo4EAAAOD6m4T+OkyT/kedNO0iZZ0WRp/iP3vkQ3UfHRwAAAIdjV/+h/ETeNJ203sLyGaWup9V2FVcw99d72v4j1i6zqbLmo7gPUImYX89nVVborPqHiBy3zPVHXcfNZnlZd8Qo86zYmH00VDhkNrNCHSr04Ww0UlJYliLqMTAgAABwjAzgP1obYVbzEf8h7q/3FOMf/vZSsi9h8pmdHyOfs7zenJKfMrf3T/AfqidomWfFRvcR3RRZm58y91plytz3H+1Ozv5lNKZS5wEAAOCY+P797eJXv2y1ZfyjJZ9VqiqMxz/8/fWeaf7D9AEpbAq9v/7upsq8uIhVWTvH3VSZkeFU/1GVeZbnebEJuQHfQUj+w+rHmhm5iEY4CIAAAMARMrz/UP9M9x+x+j68vaMfiY9hNbTp6UxW8h9WG03Uf7Q+wQx7zJrgT9vQIvRKxX8AAMBJM6j/MMIDSf7DDifUeyb7j7YDRyL1oY3M+MkOG//w4xo6ImJajn3HP2h/AQCAI2NI/2G2ieh+FaqXqOQ/hDaUZP+h+mr0e68vq1lelbkbgLHaZRw/IRmU2veU3f1PZf9R9wNpPyrzneIf9D8FAIDpMYD/0INBTDNhDE4pvf6n/v7W9pn2FqHtlT1EpWMcbEMujWfJp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8/OrMnJxJ+4PtPrbQS7nzuDek/Uu5JN4fiVcB/mOUVCqQbH+nSD+7f3FWhB430UH5WAwHa6+onHsyzm5rVg8TdzQ6+CS0R0YaPzqdSSp678r9jNuTTtyrOtEyKn4q5jfiPT1L1H89qyiFCeVbtrFaGjdC0fMXdG9seQOv4EulX4BRy6JTtQ8eOEr9n4u0v3k9yfWU99erxL53lnPI4jqTQ90cRSHmo39T64d79uhBakB5c6WUS/9mKF6ur0urs/9H923dOzU3HOOXE+6Hrfks6Te+BH3tqpZVwx3NP/K7hzq3A2CDPeeHB6F8F/Id9T4SDaW2fOL+bZL2/Dn/Vd1XdISjY/9TM0qfVwt7TzUxnnp1KaD43Rzau285Bt7HzFXdLffAJCRolkJ7/HbMhnL59IR50P61YhSdtEXIb8x/O5BbNP2NZDRxCE60h3j6uLp3pNzpvKufG9mfsaBpHxF+B+7gJnLJ76PhR2tMJ3z+BKyj+hM2NQpNQx10dyEM8hUiWgs/0uZtan+dA/DdlJNI+vnRlEHtw9fFk/s/WdZndiVg/1XC/gdTfvndqzhPG73rfeT+kVxnR6tx54PuPAimF2IMxkkL4u205LK8SDHevW9q4lKGrcDb+AyG0o2JOC01PsejCzinPr+53GnP+Fu38gXZQrNlobOE/EEIpwn+cmvrNf5qs+6WKMz2Ee2+kJbKHvKHm6hw+J9/wHwihNOE/UETGJPdH83qNGh3p1cF/IIQQQmhs4T8QQgghNLbwHwghhBAaW/gPhBBCCI0t/AdCCCGExtb0/AdCCCGETkb4D4QQQgiNLfwHQgghhMbWsfsPhBBCCJ2D8B8IIYQQGlsH8x+/+cc/vv7pfx38/BFCCCE0vg7mP/7w5d/+9X/8z++vWBCEEELo7HQw//F//u/Pv/v9f//1b/6AEEIIoXPTwfwHAAAAnCTfv79d/OqXrf7xt7/zhf8AAACAIcF/AAAAwNik+I//D3YumQzIzQquAAAAAElFTkSuQmCC" alt="" />

但是现在我只想传入y, 不想传入 x ,该怎么办呢,那么就要用到 C#的 具名参数。

三. 具名参数

具名参数的使用主要是体现在函数调用的时候。

public void Main()
{
//调用
Demo3(); // x,y不传入实参时,x,y使用默认值 "demo",5
Demo3("similar"); // y不传入实参时,y使用默认值5
Demo3("similar", ); // x,y都传入实参 // 具名参数的使用
Demo3(y:);
}

通过具名参数,我们可以指定特定参数的值,这里我们通过 Demo3(y:10)就可以解决我们上面遇到的问题(x使用默认值,y使用实参)。

注: 当使用 具名参数时,调用方法可以不用管参数的声明顺序,即如下调用方式也是可以的:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAjwAAABQCAIAAACSxDjFAAAO/klEQVR4nO2dPZKrOhOG2QQ7YQdeinLnN5sNENobcOhsqggcuWYFkziiXDXx3GCCbwN8gUBSt7pB4uf4Mud9ynXvDINBCOgHtdo+xb///g8vvPDCCy+8dvEqOgAAAGAnQFoAAAB2A6QFAABgN0BaO6IxRVW3r24FWJnGFKZ5dSMA2AuQVhptXRWeLdRB9iAEsbautlOW3Tnfa2O2POBcbsdLWX7cgh+meX4eykt5/Erdh7D+z+lwKctLmbUdCb39bV2JpxwAEANppdHWlQ8rjZG9shaNiTa/obIaUxRVXfN92sXtsPPXB9Xn6d0H/cPnM+k9y6XVczsuldZY+/8bPQzAHoC00iDS2njYE299W2WZpotFSVORgcFexvP0bmO9++FPso609PZve0UB8HuAtNJg0qJRXEijNaYwjV1uGpt8c38LE4HS03WCs6Inc2aVxuTm9Ji0yK99g+eMBFjekbcrv50SX8dyyOD5tJuc1rsdL8ebXf/jZsdVvT8m0oCStIL9+j/9nA6X483mAC9l+X5KtKtorXX6B4DfBKSVBpeWDzFhePfqGKLNELTd+kQ3zD1OfixMSQUYVDJ8jTWkZd/eN5Effwb+IOPx2gpB+ed0uBxOP9qfmWxuRysSqyjrLeIVbUQVLf85HZwgv46la0MvP/trzqBQ6mJICwAOpJWGKi2qi3BxYRq/IPw/HyFFMmAukzNHwTvXSC1J0mrqqgjcNXvOxbYvmjVbh5/TYWyKS5DW8curbr60+F+JtNyat4/UmhEkC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JRU5ErkJggg==" alt="" />

在调用含有可选参数的方法时,vs中会有智能提示,提示哪些是可以选参数及其默认值,中括号表示可选[]:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAe8AAABHCAIAAACh01itAAAJR0lEQVR4nO2cwY7bNh6H/QLtbU4L6Cn8BgHkB9FNMPIY3YNgYy99gtxswIcCOuS06G0MZKYBFsHseqYost1GTnZSFM0haPeQPdiSSOpPSralkUV/H35IZmxLpCnyM0XJM/oCAADDZ9R3BQAAoAWwOQCAD2BzAAAfwOYAAD6AzQEAfACbAwD4ADYHAPABbA4A4APYHADAB7A5AIAPYPO2SKPReLZ5yuKi9MlKA4Czxwubb2bjUUkXTtVKECy6mY1PL3dXiLn3NJLf2GY2FqsCAJeJNzYvvJZGsnDbIo0qu2/B5Wk0Go1nM3Pfu4c3eSH6k/gcAEr8s3lLE2VnYdre23F5lH6pflLoqzeK2tsrGgA8wUeb69oTVirSaBSlu8ejdLe+UTynrqlI894GMq/MmQ0Np5FtPciwufbrvmJ6nUSd2/cPAP7ipc1Lx6k+LJ2a6y5fqC5er3nYkHLxqWB4Urr6qVvZfMUhNt+9bF8V830Kb929fwDwF79trntUfXgUpeUD6v/mnLoyPzckLy92KFseshoi2TydjUeK1NtfsgcAL/DS5vksXL/XpZxY22xu2lua+H4RFkAEnRabWvYhI6y0qLPsbi7AAoAX+GjzYknFprqmNhfn5saM31bG/vHNbHyAbI0ChdMAszoHfVgAgMd4Z/NNuTBhvYfPZnP97sbqLSTiw9bZcRqNxlEkyLzxunnNHYq2wvPru9X9A4C/eGNz25d7pG8WWW3+Rb0FxpiAW+90sd72bbvzvWrz6pKQdI9NdVfyJ4l49wsAeI4XNu8bxyy+S6Xa18y7/gYVAJwf2LwVhPm5RfGtFinvX1tsAoALAZu3RXltdL9Y061PLX91y7wNBgAuBWwO3vLpL58IKdJ3f+wcbA7e0rs+yFml7/7YOaMPH34fer766uu7f/58+/rh1e09IUV61wc5q6h94/b1w5u7f2fZb73rq8X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D2/ywvu3zcO335U2fPbDQ6NtTrf5npfPT7W5q/7n0cIAdfwfmp96XOwvkaQAAAAASUVORK5CYII=" alt="" />

出处:https://www.cnblogs.com/similar/p/5006705.html

===================================================

可空参数是C#2中就引入的概念了,比如属性可以这样定义:

public decimal? Price { get; set; }

而方法参考可以这样定义:

public void GetProduct(string name, decimal? price = null)

使用:

调用属性时,可以直接为Price赋值为null

调用方法时,只传name参数,而price则默认为null了。

下面看看我的一个综合应用吧!

 public DateTime? StartDate { get; set; }
public DateTime? EndDate { get; set; } public GetProduct(string cName = default(string), ......, ResultEnum? secResult = default(ResultEnum?), DateTime? startDate = default(DateTime?), DateTime? endTime = default(DateTime?))
{
if (startDate == null)
{
throw new InvalidDataException("startDate is a required property for GetProduct and cannot be null");
}
else
{
this.StartDate = startDate;
} // 其他代码逻辑
}

这里的属性使用的是可空参数,如:StartDate 和 EndDate

而构造函数这里只列出了部分代码,我的这个参数大概有20个左右,中间的参数省略掉了,自行脑补。而函数内部又有对startDate参数的必须提供,否则抛出异常,其他的验证逻辑我就不贴出来了

调用方式:

new GetProduct(startDate : DateTime.Now.AddMonths(-1), endTime : DateTime.Now)

C#中的参数和调用方式(可选参数、具名参数、可空参数)的更多相关文章

  1. springboot的yml配置文件里有多个参数的调用方式

    本篇教程适用于配置文件一级下有多个二级或三级时,如何在service层不用通过多个@Value就能获取配置文件的参数 例如yml配置是这样的: 如果service层要引用这些参数,用多个@Value( ...

  2. Android的Fragment中onActivityResult不被调用

    1.检查该Fragment所属的Activity中,是否重写了onActivityResult方法. 2.检查Fragment中的startActivityForResult的调用方式. 请确保不要使 ...

  3. 在webapi中为Action使用dynamic参数实现Post方式调用

    1.在webapi中使用controller/action/id的路径配置,打开文件[App_Start] -[WebApiConfig] config.Routes.MapHttpRoute( na ...

  4. Hybrid App开发模式中, IOS/Android 和 JavaScript相互调用方式

    IOS:Objective-C 和 JavaScript 的相互调用 iOS7以前,iOS SDK 并没有原生提供 js 调用 native 代码的 API.但是 UIWebView 的一个 dele ...

  5. Android开发中怎样调用系统Email发送邮件(多种调用方式)

    在Android中调用其他程序进行相关处理,几乎都是使用的Intent,所以,Email也不例外,所谓的调用Email,只是说Email可以接收Intent并做这些事情 我们都知道,在Android中 ...

  6. C++中构造函数的手动和自动调用方式

    1,对象的构造通过构造函数来完成,和类名相同且没有返回值,这个时候只有参   数一个特性,构造函数可以自定义参数,这个参数一般而言就是对类进行初始  化来使用的:带有参数的构造函数的意义在于可以使得每 ...

  7. python中使用list作为默认参数且调用时不给其赋值的问题

    最近在写代码时发现一个有趣的地方,当python中的函数使用list作为默认参数且调用时不给其赋值时,无法通过在函数中将其赋值为[]来达到清空此默认参数的目的.按照道理来说,函数f1中的list为局部 ...

  8. OAuth在WebApi中的使用,前后台分离的调用方式

    前段时间由于公司架构服务层向WebApi转换,就研究了OAuth在WebApi中的使用,这中间遇到了很多坑,在此记录一下OAuth的正确使用方式. 1.  OAuth是做什么的? 在网上浏览时,大家都 ...

  9. 在js传递参数中含加号(+)的处理方式

    一般情况下,URL 中的参数应使用 url 编码规则,即把参数字符串中除了 -_. 之外的所有非字母数字字符都将被替换成百分号(%)后跟两位十六进制数,空格则编码为加号(+).但是对于带有中文的参数来 ...

随机推荐

  1. font-family 中文字体列表

    华文细黑:STHeiti Light [STXihei] 华文黑体:STHeiti 华文楷体:STKaiti 华文宋体:STSong 华文仿宋:STFangsong 儷黑 Pro:LiHei Pro ...

  2. SQLServer学习记录

    use TestDataBase;go -- 派生表-- 第3页,每页5条数据select * from (select ROW_NUMBER() over(order by stuId) as nu ...

  3. 剑指offer第32题:把数组排成最小的数及关于list.sort()和sorted( Iterable object )函数的相关知识

     * 解题思路:  * 先将整型数组转换成字符数组,然后将String数组排序,最后将排好序的字符串数组拼接出来.关键就是制定比较规则.  * 排序规则如下:  * 若ab > ba 则 a & ...

  4. Linux 系统运行命令 > 查看系统信息

    查看系统运行状态 一 . 查看硬件信息 - 1. cpu信息(可以通过find,whereis,locate查出路径) #cat /proc/cpuinfo 2 . 内存信息:meminfo(可以用c ...

  5. OpenGL的一些名词

    搬运自:https://learnopengl-cn.github.io/01%20Getting%20started/10%20Review/ 词汇表 OpenGL: 一个定义了函数布局和输出的图形 ...

  6. Selenium-java测试环境搭建

    1.1背景 Selenium也是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中,就像真正的用户在操作一样.支持的浏览器包括IE.Mozilla Firefox.Mozilla ...

  7. ln语法

    Linux文件系统中,有所谓的链接(link),我们可以将其视为档案的别名,而链接又可分为两种 : 硬链接(hard link)与软链接(symbolic link),硬链接的意思是一个文件可以有多个 ...

  8. L1 loss 与 MSE

    ---恢复内容开始--- 今天在训练时遇到的问题 把损失函数由 MSE 改成 L1 Loss 的时候 Loss 有了明显的下降 以前一直觉得 MSE 相对来说会更好 ,因为求导的话有标签与结果的差值作 ...

  9. css缩放的坑

    transform:scale()缩放后会往中间聚集.transform-origin: top left可以让他看上去是沿着左上角缩放. .zoom { transform: scale(.8); ...

  10. 关于下拉列表HtmlDownlistFor的使用

    1.定义下拉列表需要的数据 public static List<SelectListItem> GetProList() {   List<SelectListItem> i ...