hf

导航

 

Optional and Named Parameters

When designing a method’s parameters, you can assign default values to some of or all the parameters ,when you call a method, you can specify arguments by using the name of their parameters  .

Rules and Guidelines

  • You can specify default values for the parameters of methods, constructor methods,and parameterful properties (C# indexers)  You can also specify default values for  parameters that are part of a delegate defnition .
  • Parameters with default values must come after any parameters that do not have  default values  ,but a params array parameter must come after all parameters and the array cannot have a default value itself .
  • Default values must be constant values known at compile time  .
  • Be careful not to rename parameter variables because any callers who are passing  arguments by parameter name will have to modify their code  .
  • Be aware that changing a parameter’s default value is potentially dangerous if the method is called from outside the module  .
  • You cannot set default values for parameters marked with either the ref or out keywords because there is no way to pass a meaningful default value for these parameters .
  • Arguments can be passed in any order; however, named arguments must always appear at the end of the argument list .
  • You can pass arguments by name to parameters that do not have default values, but all required arguments must be passed (by position or by name) for the compiler to compile the code .

The DefaultParameterValue and Optional Attributes

In C#, when you give a parameter a default value, the compiler internally applies the  System.Runtime.InteropServices.OptionalAttribute and System.Runtime.InteropServices.DefaultParameterValueAttribute to the parameter and persists this attribute in the resulting fle’s metadata  Then, DefaultParameterValueAttribute’s constructor is passed the constant value that you specifed in your source code .

Implicitly Typed Local Variables

C# supports the ability to infer the type of a method’s local variable from the type of  expression that is used to initialize it (var)

Declaring a local variable using var is just a syntactical shortcut that has the compiler infer the specifc data type from an expression . The
var keyword can be used only for declaring local variables inside a method while the dynamic keyword can be used for local variables, felds, and arguments  .You cannot cast an expression to var, but you can cast an expression to dynamic . You must explicitly initialize a variable declared using var while you do not have to initialize a variable declared with dynamic .

Passing Parameters by Reference to a Method

By default, the CLR assumes that all method parameters are passed by value . When reference type objects are passed, the reference (or pointer) to the
object is passed (by value) to the method .For value type instances, a copy of the instance is passed to the method .

The CLR allows you to pass parameters by reference (ref ,out).  If a method’s parameter is marked with out, the caller isn’t expected to have initialized the object prior to calling the method .The called method must write to the value before returning . If a method’s parameter is marked with ref, the caller must initialize the parameter’s value prior to calling the method  .The called method can read from the value and/or write to the value .

Passing a Variable Number of Arguments to a Method

It’s sometimes convenient for the developer to defne a method that can accept a variable number of arguments (params).

When the C# compiler detects a call to a method, the compiler checks all of the methods with the specifed name, where no parameter has the ParamArray attribute applied  .If a method exists that can accept the call, the compiler generates the code necessary to call the method  .However, if the compiler can’t fnd a match, it looks for methods that have a ParamArray attribute to see whether the call can be satisfed  .If the compiler fnds a match, it emits code that constructs an array and populates its elements before emitting the code that calls the selected method .

Parameter and Return Type guidelines

When declaring a method’s parameter types, you should specify the weakest type possible, preferring interfaces over base classes  .

On the fip side, it is usually best to declare a method’s return type by using the strongest type possible .

Sometimes you want to leave yourself some fexibility to change what your method returns, choose a weaker return type  .

Const-ness

The CLR does not support constant objects/arguments .

posted on 2010-08-13 15:31  hf  阅读(290)  评论(0)    收藏  举报