Improve parameters

parameter order

public OperationResult PlaceOrder(Product product, int quantity, bool includeAddress, bool sendCopy)

Acted opon or key to the operation (like product)

Required for the operation

Flags (like inclludeAddress)

Optional parameters

Best Practices

Do:

Define coherent parameters names

Defind an XML document comment for each parameter

Keep the number of parameters to a minimum

Order the parameters in a logical sequence

Use a consistent parameter order

Avoid:

Unused parameters

Named arguments

public OperationResult PlaceOrder(Product product, int quantity, bool includeAddress, bool sendCopy)

var result = Vendor.PlaceOrder(product, quantity:, includeAddress:true, sendCopy:false);

Named arguments Best Practices

Do:

Use named arguments as needed for clarity when calling a method

Avoid:

Unnecessary named arguments (PlaceOrder(product:product...))

Optional Parameters

public OpertionalResult PlaceOrder(Product product, int quantity, DateTimeOffset? deliverBy = null, string instructions = "standard delivery")

Features:

Specify a default vallue

Are optional when the method is called

If argument is not provided, default is used

Can dramatically reduce the number of overloads

Notes:

Optional parameters must be defined after required parameters

When calling the method, if an argument is provided for any optional parameter, it must also provide arguments for all preceding parameters, or use named arguments.

Best Practices

Do:

Use optional parameters to minimize overload bloat

Avoid:

Optional parameters when the parameters are one or the other

Optional parameters if default could change and component versioning is important

Ref & Out

By Value or By Reference

public bool PlaceOrder(Product product, int quantity, ref string orderText)
public bool PlaceOrder(Product product, int quantity, out string orderText)

ref

Argument passed "by reference"

Argument variable must be initialized

Parameter values can be changed in the method

Changes are reflecting in the calling code

out

Argument passed "by reference"

Argument variable must be declared

Parameter values must be set in the method

Changes are reflecting in the calling code

Best Practices

Do:

Use ref when the method expects an incoming value

Use out when the method expects no incoming value

Avoid:

ref and out where feasible, return an object instead

FAQ

1.What is a named argument and when should it be used?

A named argument uses the parameter name when calling the method

Used to clarify the purpose of an argument and define arguments without concern for there position in the parameter list

2.How is an optional parameter defined?

By sepecify a default value

3.What is difference between passing an argument by value vs by reference?

When passed by value (which is default), the value of the argement is passed to the method.

When passed by reference (use ref or out), the variable is effectively passed to the method.

Because of this, passing by reference enables the method to change the value of the parameter and have that changed reflected in the calling code.

4.What is the difference between ref and out?

A ref parameter requires that the argument be initialized before it is passed.The method can modify the value for the ref parameter.

A out parameter must be declared,but not initialized before it is passed.The method must provide a value for the out parameter.

C# Best Practices - Specify Clear Method Parameters的更多相关文章

  1. Core Java Volume I — 4.5. Method Parameters

    4.5. Method ParametersLet us review the computer science terms that describe how parameters can be p ...

  2. Part 67 to 70 Talking about method parameters in C#

    Part 67 Optional parameters in c# Part 68  Making method parameters optional using method overloadin ...

  3. 【spring data jpa】repository中使用@Query注解使用hql查询,使用@Param引用参数,报错:For queries with named parameters you need to use provide names for method parameters. Use @Param for query method parameters, or when on

    在spring boot中, repository中使用@Query注解使用hql查询,使用@Param引用参数 如题报错: For queries with named parameters you ...

  4. DataSet.Clear Method ()

    Clears the DataSet of any data by removing all rows in all tables. 需要注意的是这个方法不会清空DataSet中的DataTable, ...

  5. jQuery基础教程-第8章-003Providing flexible method parameters

    一.The options object 1.增加阴影效果 (function($) { $.fn.shadow = function() { return this.each(function() ...

  6. 动态linq表达式新方法,Dynamic LINQ Extension Method

    Remember those old posts on Dynamic LINQ? You are probably aware that Microsoft has made its impleme ...

  7. Natural language style method declaration and usages in programming languages

    More descriptive way to declare and use a method in programming languages At present, in most progra ...

  8. Auto Clear Unity Console Log

    功能 可以在Editor模式下执行,当然也可以Runtime模式下执行,自动清除 Console的log信息 功能需求 当在制作Editor的一些功能时,常常需要手动的点击Console窗口的Clea ...

  9. 9.Parameters

    1.Optional and Named Parameters calls these methods can optionally not specify some of the arguments ...

随机推荐

  1. Ubuntu12.04获取root权限

    有的时候我们需要Ubuntu的root权限,我们该如何获取呢? 其实,很简单,我们只需要在终端中输入以下命令即可获得root权限. 第一步,打开终端 ( ctrl+alt+T ) 第二步,输入命令:s ...

  2. ora-01653: unable to extend table sys.aud$ by 8192 in tablespac system[转载]

    在用sqlplus user/password@truth登录数据库时报如下错误:ORA-00604: error occurred at recursive SQL level 1ORA-01653 ...

  3. setFocus一定要写在setLayout设置的后面,否则不起作用——使用setFocusPolicy为控件设置不同的焦点策略:Tab焦点,Click焦点,Wheel焦点和没有焦点

    QLineEdit* pEditor = new QLineEdit(m_strText);    pEditor->resize(.......);    pEditor->move(. ...

  4. 传智播客C/C++各种开发环境搭建视频工具文档免费教程

    传智播客作为中国IT培训的领军品牌,一直把握技术趋势,给大家带来最新的技术分享!传智播客C/C++主流开发环境免费分享视频文档中,就有写一个helloworld程序的示范.火速前来下载吧 所谓&quo ...

  5. MFC 动态创建控件

    动态控件是指在需要时由Create()创建的控件,这与预先在对话框中放置的控件是不同的.   一.创建动态控件:   为了对照,我们先来看一下静态控件的创建.   放置静态控件时必须先建立一个容器,一 ...

  6. oracle 11g RAC ocfs2

    http://oracle-base.com/articles/linux/ocfs2-on-linux.php http://oracle-base.com/articles/11g/oracle- ...

  7. Linux2.6中的Slab层

          还记得一个进程创建的时候是什么给它分配的“进程描述符”吗?没错,是slab分配器,那么,这个slab分配器是个什么东西呢?       分配和释放数据结构是所有内核中最普遍的操作之一.为了 ...

  8. 一、Cocos2dx在visualStudio或者vc++中环境搭建(入门篇)

    本文由qinning199原创,转载请注明:http://www.cocos2dx.net/?p=106 0.概述 Cocos2dx-win32的项目能够被向导生成 向导支持vs2008,vs2010 ...

  9. block 解析 - 内存

    block结构体相应的也有一个成员引用,这样会增加对局部变量的 _para1引用,在Block销毁的时候引用就释放掉了 我们了解到了用__block修饰的变量,可以在block内部修改,__block ...

  10. linux下curl编程

    LibCurl是免费的客户端URL传输库,支持FTP,FTPS, HTTP, HTTPS, SCP, SFTP, TFTP, TELNET, DICT, FILE ,LDAP等协议,其主页是http: ...