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. python request模块学习

    安装: pip install requests 使用: import requests HTTP请求:GET.POST.PUT.DELETE.HEAD.OPTIONS 1) get res = re ...

  2. Net Core- 配置组件

    Net Core- 配置组件 我们之前写的配置都是放置在配置文件Web.config或者app.config中,.net core提供了全新的配置方式,可以直接写在内存中或者写在文件中. .Net C ...

  3. Word2Vec在Tensorflow上的版本以及与Gensim之间的运行对比

    接昨天的博客,这篇随笔将会对本人运行Word2Vec算法时在Gensim以及Tensorflow的不同版本下的运行结果对比.在运行中,参数的调节以及迭代的决定本人并没有很好的经验,所以希望在展出运行的 ...

  4. Azure SQL 数据库引入了新的服务级别

     新的级别将提升客户体验,并提供更多的业务连续性选项 为了更好地满足您在灵活性提升方面的需求,MicrosoftAzure SQL 数据库添加了新的服务级别(基础级和标准级),以与当前处于预览状态 ...

  5. Android使用XML全攻略(2)

    Android使用XML全攻略(2)   Android 是针对移动设备的一种新兴的开源操作系统和 SDK.借助它,您可以创建功能强大的移动应用程序.当您的应用程序可以访问 Web 服务时,其吸引力会 ...

  6. Windows Azure 社区新闻综述(#72 版)

    欢迎查看最新版本的每周综述,其中包含有关云计算和 Windows Azure的社区推动新闻.内容和对话. 以下是过去一周基于您的反馈汇集在一起的内容: 文章 ·   在 Windows Azure 移 ...

  7. iOS 中UITableViewController 中tableView 会被状态栏覆盖的问题

    解决办法在 生命周期函数viewDidAppear中设置即可 - (void)viewDidAppear:(BOOL)animated { self.tableView.frame = CGRectM ...

  8. js页面跳转 和 js打开新窗口 方法

    js页面跳转 和 js打开新窗口 方法 第一种: 第二种: 第三种: 第四种: 第五种: 1.在原来的窗体中直接跳转用 window.location.href="你所要跳转的页面" ...

  9. 从零开始写驱动——vfd专用驱动芯片HT16514并行驱动程序编写

    前言 一直看别人搞的 vfd 很漂亮,前段时间淘了个 vfd 模块来,但没有模块资料,还好芯片没有打磨的,良心商家啊.周末抽空来研究一下这个东西. 从零开始 打开外壳 测试线路 查看芯片是 HT165 ...

  10. Android应用开发基础篇(5)-----Handler与多线程

    链接地址:http://www.cnblogs.com/lknlfy/archive/2012/02/19/2358155.html 一.概述 Handler这个类主要用来发送和处理消息的.它有多个发 ...