C# Best Practices - Specify Clear Method Parameters
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的更多相关文章
- 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 ...
- 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 ...
- 【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 ...
- DataSet.Clear Method ()
Clears the DataSet of any data by removing all rows in all tables. 需要注意的是这个方法不会清空DataSet中的DataTable, ...
- jQuery基础教程-第8章-003Providing flexible method parameters
一.The options object 1.增加阴影效果 (function($) { $.fn.shadow = function() { return this.each(function() ...
- 动态linq表达式新方法,Dynamic LINQ Extension Method
Remember those old posts on Dynamic LINQ? You are probably aware that Microsoft has made its impleme ...
- 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 ...
- Auto Clear Unity Console Log
功能 可以在Editor模式下执行,当然也可以Runtime模式下执行,自动清除 Console的log信息 功能需求 当在制作Editor的一些功能时,常常需要手动的点击Console窗口的Clea ...
- 9.Parameters
1.Optional and Named Parameters calls these methods can optionally not specify some of the arguments ...
随机推荐
- 几本不错的开源书(to be continued)
Linux 1.working-on-gnu-linux GNU/Linux 至今已經相當成熟並足以應付日常生活之使用,凍仁也於 2009 年開始使用它來工作至今,將藉由此書 1 來撰寫較有系統的文章 ...
- MySQL学习系列一---命令行连接mysql和执行sql文件
1.命令行连接mysql #mysql -h(主机) -u(用户名) -p (数据库名) mysql -hlocalhost -uroot -p testdb Enter password: **** ...
- HortonWorks
http://zh.hortonworks.com/products/hortonworks-sandbox/
- FTL(Flash translation layer)闪存转换层
前面说过,闪存的读写单位为页,而页的大小一般为4KB或8KB,但我们的操作系统读写数据是按HDD的扇区尺寸进行的(512Byte(字节)),更麻烦的是闪存擦除以块作单位,而且未擦除就无法写入,这导致操 ...
- VC++中的类的内存分布(上)(通过强制转换,观察地址,以及地址里的值来判断)
0.序 目前正在学习C++中,对于C++的类及其类的实现原理也挺感兴趣.于是打算通过观察类在内存中的分布更好地理解类的实现.因为其实类的分布是由编译器决定的,而本次试验使用的编译器为VS2015 RC ...
- crtmpserver初探
前言 Adobe的FMS(Flash Media Server)是很好用.但对应着分级授权的是money和有限功能开放.商业的东西既然用不起,也阻碍了我们的技术进步,那就只能求助于开源社区 ...
- mysql 批量删除分区
alter table titles drop partition p01; use zabbix; mysql> source drop_par.sql [oracle@oadb mysql] ...
- UVa1584 Circular Sequence
#include <stdio.h>#include <string.h> int less(char* str, size_t len, size_t p, size_t q ...
- 多线程笔记--原子操作Interlocked系列函数
前面写了一个多线程报数的功能,为了描述方便和代码简洁起见,只输出最后的报数结果来观察程序运行结果.这非常类似一个网站的客户访问统计,每个用户登录用一个线程模拟,线程运行时将一个表示计数的变量递增.程序 ...
- 5.6.3.7 localeCompare() 方法
与操作字符串有关的最后一个方法是localeCompare(),这个方法比较两个字符串,并返回下列值中的一个: 如果字符串在字母表中应该排在字符串参数之前,则返回一个负数(大多数情况下是-1,具体的值 ...