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 ...
随机推荐
- HTML禁止使用右键
<html> <script type="text/javascript"> <!-- document.oncontextmenu=function ...
- get the execution time of a sql statement.
declare @d datetimeset @d = GETDATE()select * from dbo.spt_valuesselect [语句执行花费时间(毫秒)]= DATEDIFF(ms, ...
- jquery 小插件,完成“输入字段预期值的提示信息”,防html5 placeholder属性
前言:在很多时候,我们需要文本框中显示默认值,获取焦点时,文字框中就会清空给的值,当失去焦点时,如果没有值,继续显示默认的文字,如果有输入值,就显示输入的值.现在项目中需要用到这个地方的功能比较多,于 ...
- IC封装
1.QFN •QFN—Quad Flat No-lead Package 四方无引脚扁平封装 2.SOIC •SOIC—Small Outline IC 小外形IC封装 3.TSSOP •TSSOP— ...
- CCNA实验(9) -- Frame Relay
帧中继的一些特点:1.中小企业常用的广域网线路2.通信费用较低3.配置较为复杂 1.将Cisco路由器配置为帧中继交换机2.帧中继基本配置.帧中继映射3.在帧中继的链路上运行RIPv24.帧中继的多点 ...
- 关于结构体和C++类的内存地址问题
关于结构体和C++类的内存地址问题 今天终于有时间写点东西了~ 太爽了 *_* 很多人都知道C++类是由结构体发展得来的,所以他们的成员变量(C语言的结构体只有成员变量)的内存分配机制是一样 ...
- IE 中开发,兼容与性能测试工具汇总
前言 对于开发者来说, IE的兼容性是最让人头疼的. 因为是微软的产品, 且绑定在操作系统上, 所以IE的占用率还是相当大, 对于开发者来说, 这部分的兼容的考虑就不可避免了. 对于IE 的各版本来说 ...
- android UI进阶之用【转】
android UI进阶之用ViewPager实现欢迎引导页面 摘要: ViewPager需要android-support-v4.jar这个包的支持,来自google提供的一个附加包.大家搜下即可. ...
- Linux系统源码安装过程中的prefix选项
在linux和unix环境中,源码安装是最常用的软件安装方式,一些软件除了提供源码外,也提供各种发行版的二进制安装包(如基于redhat包管理工具的rpm包),但强烈建议使用源码安装方式.原因是:(1 ...
- Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (一)
作为一个完成的应用程序,数据存储操作是必不可少的.因此,Android系统一共提供了四种数据存储方式.分别是:SharePreference.SQLite.Content Provider和File. ...