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 ...
随机推荐
- glib源码安装使用方法
glib库是GTK+和GNOME工程的基础底层核心程序库,是一个综合用途的实用的轻量级的C程序库,它提供C语言的常用的数据结构的定义.相关的处理函数,有趣而实用的宏,可移植的封装和一些运行时机能,如事 ...
- 11-3URLTestDemo实例操作完成URL单元测试
11-3URLTestDemo 1.File -> New -> Project 在左边模板中选择Visual C#里的Web,对应到的项目类型选择ASP.NET MVC3 Web App ...
- 如何使用Excel和Word编辑和打印条形码
本文介绍如何使用Microsoft Office Excel 2007和Microsoft Office Word 2007进行条形码的编辑后,通过普通的办公打印机将条形码打印出来. 对于少量,简单的 ...
- linux系统CPU,内存,磁盘,网络流量监控脚本
前序 1,#cat /proc/stat/ 信息包含了所有CPU活动的信息,该文件中的所有值都是从系统启动开始累积到当前时刻 2,#vmstat –s 或者#vmstat 虚拟内存统计 3, #cat ...
- HDU 5054 Alice and Bob
#include <cstdio> int main(){ int n,m,x,y; while(~scanf("%d%d%d%d",&n,&m,&am ...
- java单链表代码实现
用惯了C++,java写起来果然不太爽...不废话了,上代码... package javaInnerclassDemo; class Link{ class Node{ private String ...
- C# DateTime.Now 用法小记
1.DateTime.Now 获取时间跟系统当前时间一直并且格式一直,如系统时间带有星期几,获取的时间也会带有 2,以下为拷贝前人总结的: //2008年4月24日 System.DateTime ...
- HTTP初步注解
搜集了一下网上的资源和自己看过的一些书,小小总结了一波HTTP,现在也只是很肤浅的了解,期望以后深入理解后能写出更有营养的笔记. HTTP协议的主要特点 + 支持客户/服务器模式.+ 简单快速:客户向 ...
- Sublime Text3快捷方式总结
Ctrl+P快速查找 Ctrl+D多行游标//同时选中多个地方进行编辑 Ctrl+F查找替换 Ctrl+H查找替换 Ctrl+G快速跳到某一行 Ctrl+shift+P命令模式 在命令模式下设置语法: ...
- BZOJ 2752: [HAOI2012]高速公路(road)( 线段树 )
对于询问[L, R], 我们直接考虑每个p(L≤p≤R)的贡献,可以得到 然后化简一下得到 这样就可以很方便地用线段树, 维护一个p, p*vp, p*(p+1)*vp就可以了 ----------- ...