CLR Via C # Reading Notes-Parameters

Source: Internet
Author: User

. NET Framework reader record 1 parameter

  • Default Value and name parameter

In normal work, when designing a method, the design of method parameters must be carefully considered in method design, such as the type, number, default value, and optionality of parameters. Designing a good method plays a major role in code readability, maintainability, and scalability. In C #, the default value of the parameter is set as follows:

 

The default setting method of the parameter is shown in the red rectangle. Function Main calls the ShowInfor function four times. After carefully observing the ShowInfor function call, we can find a feature as follows:

Program running result:

 

The following conclusions can be drawn from the program running result:

  1. The order in which a function transmits values by named parameters is not the same as that defined by the function.
  2. The value displayed to the parameter must be on the left side of the value passed by the named parameter.

If you add a parameter ID to the ShowInfor function, it is defined as follows:

 

The error message of VS indicates that the order of the optional parameters must be after the required parameters.

U Out and ref

There are two ways to pass a parameter: pass a value or pass a reference. for basic data types, it is generally passed by value. If you want to pass a parameter by reference, what should I do? Here we will talk about the Out and Ref related knowledge in C.

For the Out parameter, the passed real parameter can not be initialized, but the parameter must be assigned a value before the function is returned. The parameter can be written inside the function, as shown below:

 

If no value is assigned to the Out parameter, as shown in the following code:

 

For a Ref parameter, the passed real parameter must be initialized before being passed to the form parameter. It can be read and written inside the function.

public static void GetName(ref string name)
{
name = "SuperMan";
}

If the real parameter is not initialized, the following error occurs: a variable with no value assigned is used.

The Out and Ref parameters must be of the same type as the passed real parameters; otherwise, the following error occurs:

U passed the Variable Number Parameter params

Generally, the most frequently used method is the fixed number of parameters. In fact, in C #, you can pass any number of parameters. This provides a good solution without knowing how many parameters may exist in the future before designing the method. It mainly relies on the keyword params. Params must be used together with array; otherwise, it is shown as follows:

 

Are you sure you want to call the variable parameter quantity function or the fixed parameter quantity function? C # The Compiler first checks whether a method prototype that completely matches the call function exists. If yes, it calls the method directly. If the method does not exist, check whether a method using the ParamArray attribute exists. If yes, call this method. The entire process is as follows:

 

The above are the parameter-related notes. I seldom write a blog. If there are any mistakes, please point out that I have made some corrections. Thank you.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.