当前位置:首页 > 服务端 > C# Best Practices

C# Best Practices

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:12, 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.

来源链接:https://www.cnblogs.com/lmfy/p/5150754.html

版权声明:
1、Java侠(https://www.javaxia.com)以学习交流为目的,由作者投稿、网友推荐和小编整理收藏优秀的IT技术及相关内容,包括但不限于文字、图片、音频、视频、软件、程序等,其均来自互联网,本站不享有版权,版权归原作者所有。

2、本站提供的内容仅用于个人学习、研究或欣赏,以及其他非商业性或非盈利性用途,但同时应遵守著作权法及其他相关法律的规定,不得侵犯相关权利人及本网站的合法权利。
3、本网站内容原作者如不愿意在本网站刊登内容,请及时通知本站(javaclubcn@163.com),我们将第一时间核实后及时予以删除。





本文链接:https://www.javaxia.com/server/125124.html

标签: unused variable
分享给朋友: