Posts

Showing posts from November, 2017

FAQ

What is the difference between a parameter and argument? A parameter is part of the signature An argument is part of the method call 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 their position in the parameter list. How is optional parameter defined? In the method signature, we define a parameter as optional by specifying a default value. What is the difference between passing an argument by value vs by reference? When passed by value , which is the default, the value of the argument is passed to the method. When passed by reference using ref or out, the variable is effectively passed to the method.   Because of this, passing by reference enable the method to change the value of the parameter and have that change reflected in the calling code. What is the difference between ref and out ? ...

Named Arguments

Image
We can use parameters names from the method signature in the method call as named arguments. To take advantage of named arguments, simply add the parameter name and a colon before argument value. But there are some eccentricities we need to keep in mind. when using arguments, parameter order no longer matters, c# match the arguments by their names not by their by positional order. Not all arguments need to be named, we can use named arguments for only those arguments that are less clear. All name arguments must follow positional arguments.

Improving Parameters in the method signatrue

Image
When working to improve method parameters, our goal is to define the parameters in our method signature, such that the resulting method call arguments are clear, easy to understand, and less prone to error. The first step toward that goal is to define coherent parameter names that clearly describe the purpose of each parameter. Another way we can indicate the intent of each parameter is to add XML document comment. In addition, to specify the method summary, we can add a param tag for each parameter and provide detailed parameter description. This information appears in the intelisence, helping any developer using our method in the appropriate arguments. Limiting the number of the parameters can also make method easier to use. Defining a logical and consistent parameter order can also make our method signature easier to use. One recommended technique is to start with the parameters that are acted upon or are key to the operation, in o...

Introduction

Methods are the workhouse of our application. But without clear parameters, it is easy to get off track. Parameters that are unclear cause confusion an misuse , making development more difficult and causing hard to find bugs .  The arguments provide the value for each parameter.