1.可选参数

可选参数是.NET4中新添加的功能,应用可选参数的方法在被调用的时可以选择性的添加需要的参数,而不需要的参数由参数默认值取代。

class Program
{
/// <summary>
/// 可选参数 命名参数
/// </summary>
static void Main(string[] args)
{
Console.WriteLine(ShowComputer());
Console.WriteLine(ShowComputer("P5300","1G"));
Console.Read();
} private static string ShowComputer(string cpu = "i3 370M", string ram = "4G", string disk = "320G")
{
return "My computer ... \nCpu:" + cpu + "\nRam:" + ram + "\nDisk:" + disk + "\n";
}
}

  

代码运行的结果图下图:

2.命名参数

命名参数是把参数附上参数名称,这样在调用方法的时候不必按照原来的参数顺序填写参数,只需要对应好参数的名称也能完成方法。

class Program
{
/// <summary>
/// 可选参数 命名参数
/// </summary>
static void Main(string[] args)
{
Console.WriteLine(ShowComputer("i3 370M","2G","320G"));
Console.WriteLine(ShowComputer(disk: "320G", cpu: "i3 370M", ram: "2G"));
Console.Read();
} private static string ShowComputer(string cpu, string ram, string disk)
{
return "My computer ... \nCpu:" + cpu + "\nRam:" + ram + "\nDisk:" + disk + "\n";
}
}

    命名参数如果只是改变参数的顺序,这样的意义并不大,我们没有必要为了改变顺序而去用命名参数,他与可选参数结合才能显示出他真正的意义。

class Program
{
/// <summary>
/// 可选参数 命名参数
/// </summary>
static void Main(string[] args)
{
Console.WriteLine(ShowComputer(ram: "3G"));
Console.Read();
} private static string ShowComputer(string cpu = "i3 370M", string ram = "2G", string disk = "320G")
{
return "My computer ... \nCpu:" + cpu + "\nRam:" + ram + "\nDisk:" + disk + "\n";
}
}

  程序只赋值了第二个参数ram,其他参数均为默认值,运行结果大家应该都知道了。这样命名参数和可选参数都发挥了他们独特的作用。

C# 可选参数 命名参数的更多相关文章

  1. Dart 知识点:位置参数(必选)、位置参数(可选)、命名参数(都是可选)

    先后顺序:位置参数(必选).位置参数(可选).命名参数(都是可选) 位置参数(可选).命名参数(都是可选),不能同时使用

  2. mybatis3.1-[topic-18-20]-_映射文件_参数处理_单个参数&多个参数&命名参数 _POJO&Map&TO 三种方式及举例

    笔记要点出错分析与总结 /**MyBatis_映射文件_参数处理_单个参数&多个参数&命名参数 * _POJO&Map&TO 三种方式及举例 _ * 单个参数 : #{ ...

  3. C#4.0新特性:可选参数,命名参数,Dynamic

    1.可选参数 可以为方法的参数设置一个默认值,如下: class Program { static void Main(string[] args) { Show(); Show("cary ...

  4. c# 方法参数(传值,传引用,ref,out,params,可选参数,命名参数)

       一.方法参数的类型----值类型和引用类型 当方法传递的参数是值类型时,变量的栈数据会完整地复制到目标参数中即实参和形参中的数据相同但存放在内存的不同位置.所以,在目标方法中对形参所做的更改不会 ...

  5. C# 4.0 新特性dynamic、可选参数、命名参数等

    1.dynamic ExpandoObject熟悉js的朋友都知道js可以这么写 :   1 var t = new Object(); 2 t.Abc = ‘something’; 3 t.Valu ...

  6. c#中的可选参数和命名参数的使用

    C#4.0之后出现了一个可选参数这个特性. class Cal { static void Main(string[] args) { test1 t = new test1(); t.Add(, ) ...

  7. c#方法重载,可选参数,命名参数。

    其实这里没什么可说哦,c++的语法大同小异.先看一段代码. class Program { public static void Test(int a) { Console.WriteLine(&qu ...

  8. C#方法的六种参数,值参数、引用参数、输出参数、参数数组、命名参数、可选参数

    方法的参数有六种,分别是值参数.引用参数.输出参数.参数数组.命名参数.可选参数. 值参数 值参数是方法的默认类型,通过复制实参的值到形参的方式把数据传递到方法,方法被调用时,系统作两步操作: 在栈中 ...

  9. C# 方法的可选参数、命名参数

    原文 http://www.cnblogs.com/lonelyxmas/admin/EditPosts.aspx?opt=1 C#方法的可选参数是.net 4.0最新提出的新的功能,对应简单的重载可 ...

随机推荐

  1. LintCode: Combination Sum II

    C++ DFS class Solution { public: void help(vector<int> &a, int now, int sum, int target, v ...

  2. LintCode: Number of Islands

    分析:经典连通分量问题 图: 节点:所有1的位置 边:两个相邻的1的位置有一条边 BFS/DFS (DFS使用递归,代码较短) 选一个没标记的点,然后搜索,扩展4个邻居(如果有),直到不能扩展 每一次 ...

  3. 【树莓派】树莓派与XBMC及Kodi、LibreELEC插件(二)

    之前的相关文章参考: [树莓派]树莓派与XBMC及Kodi.LibreELEC插件(一) [树莓派]树莓派与XBMC及Kodi.LibreELEC插件(二) [树莓派]树莓派与XBMC及Kodi.Li ...

  4. strus2 struts.xml详解

    <struts> <!-- 配置一个包:package --> <package name="demo1" extends="struts- ...

  5. python 爬虫资料

    API Requests PyQuery http://www.tuicool.com/articles/UZrmUb2 http://blog.csdn.net/cnmilan/article/de ...

  6. css 进度条

    <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...

  7. openssl、x509、crt、cer、key、csr、ssl、tls process

    今天尝试在mac机上搭建docker registry私有仓库时,杯具的发现最新的registry出于安全考虑,强制使用ssl认证,于是又详细了解linux/mac上openssl的使用方法,接触了一 ...

  8. 怎样使用Fiddler获取WebApi的token值?

    User-Agent: Fiddler Host: localhost: Content-Length: Content-Type: application/json grant_type=passw ...

  9. GoldenGate 12c 新特性 Credential Store and USERIDALIAS

    GoldenGate 12C的Credential Store and USERIDALIAS新特性有点类似存储钱夹,提高了配置的易用性和安全性. --生成credentialstore文件 GGSC ...

  10. Android开发之使用HttpURLConnection进行POST请求

    一.前提准备 在开始实际编码之前,我们有必要先了解下将会用的类以及方法,进行一个大体的了解. 1.URL类 这个类主要的功能是定位到要获取资源的网址以及打开连接.比如下面的代码: URL realur ...