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. java使用验证码进行登录验证

    随机生成4位验证码,将生成的4位数字字母数字放入session private static void outputVerifyCode(HttpServletRequest request, Htt ...

  2. sql2012管理

    一.还原完整备份的语法如下: RESTORE DATABASE { database_name | @database_name_var }                  --数据库名 [ FRO ...

  3. php基础知识总结

    PHP 代表 PHP: Hypertext Preprocessor PHP 文件可包含文本.HTML.JavaScript代码和 PHP 代码 PHP 代码在服务器上执行,结果以纯 HTML 形式返 ...

  4. Github Blog 搭建手册

    http://www.ilehao.com/blog/2012/11/11/github-blog-config/ http://www.freebuf.com/articles/web/25613. ...

  5. C# WinForm判断Win7下是否是管理员身份运行

    原文:C# WinForm判断Win7下是否是管理员身份运行 如果程序不是以管理员身份运行,操作本地文件会提示:System.UnauthorizedAccessException异常 Vista 和 ...

  6. ThinkPHP 3 的输出

    一.ThinkPHP 3 的输出 (重点) a.通过 echo 等PHP原生的输出方式在页面中输出 b.通过display方法输出 想分配变量可以使用assign方法 c.修改左右定界符 休要修改配置 ...

  7. FastCGI | FastCGI -

    FastCGI | FastCGI - FastCGI About FastCGI FastCGI is simple because it is actually CGI with only a f ...

  8. ssh的学习

    快毕业了.临走前帮导师搭建了gerrit,git服务器,其中涉及ssh的知识,就总结了下.希望对大家有帮助 一.前言(ssh出世的原因) 万物有因就有果,既然ssh存在,就必然有它存在的理由! 许多网 ...

  9. fastDFS同步问题讨论

    一.文件同步延迟问题 前面也讲过fastDFS同组内storage server数据是同步的, Storage server中由专门的线程根据binlog进行文件同步.为了最大程度地避免相互影响以及出 ...

  10. 纯JavaScript实现HTML5 Canvas六种特效滤镜

    纯JavaScript实现HTML5 Canvas六种特效滤镜  小试牛刀,实现了六款简单常见HTML5 Canvas特效滤镜,并且封装成一个纯 JavaScript可调用的API文件gloomyfi ...