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 ...
随机推荐
- No architectures to compile for (ONLY_ACTIVE_ARCH=YES, active arch=x86_64, VALID_ARCHS=armv7 armv7s)
In Build Settings are: Architectures: Starndard (armv7, armv7s) Base SDK: Latest iOS (iOS 6.0) Build ...
- Cortex-M3 动态加载二(RWPI数据无关实现)
上一篇关于动态加载讲述的是M3下面的ropi的实现细节,这一篇则讲述RW段的实现细节以及系统加载RW段的思路,我在M3上根据这个思路可以实现elf的动态加载,当然进一步的可以优化很多东西,还可以研究将 ...
- Windows Azure 新上线网络相关服务
动态路由网关.点到站点(Point to Site)VPN正式商用 动态路由网关和点到站点VPN支持基于路由的VPN,并且允许用户将独立计算机连接到Azure上的虚拟网络.现在,虚拟网络中的动态 ...
- Ubuntu下lamp(PHP+Mysql+Apache)搭建+完全卸载卸载方法
安装apache2 sudo apt-get install apache2 安装完成,运行如下命令重启下: sudo /etc/init.d/apache2 restart 在浏览器里输入http: ...
- Android二维码开源项目zxing用例简化和生成二维码、条形码
上一篇讲到:Android二维码开源项目zxing编译,编译出来后有一个自带的測试程序:CaptureActivity比較复杂,我仅仅要是把一些不用的东西去掉,用看起来更方便,二维码和条形码的流行性自 ...
- centos6.5设备mysql5.6
1. 首先检查版本号number # uname -a 要么 # cat /etc/redhat-release CentOS release 6.6 (Final) 2. 下载并安装Mysql的yu ...
- 通过配置Tomcat,让Android真机通过局域网访问PC的文件
在 Tomcat 根目录,找到conf\server.xml文件. 1.打开server.xml查找修改端口为8080端口: <Connector port="8080" p ...
- linux查看和设置系统时间 hwclock && date
http://www.linuxso.com/command/hwclock.html查看时间{1. date查看系统时钟, hwclock查看硬件时钟hwclock && date ...
- No orientation specified, and the default is
链接地址:http://jingyan.baidu.com/article/a24b33cd7722dc19fe002bd0.html No orientation specified, and th ...
- BZOJ 1601: [Usaco2008 Oct]灌水( MST )
MST , kruskal 直接跑 ---------------------------------------------------------------------- #include< ...