C # name parameter []

Source: Internet
Author: User

The named parameter (named arguments) means that you can call the parameter by specifying the parameter name when calling the function. The biggest advantage of this function is to facilitate the ordering of parameters according to the caller's needs, without the need to stick to the order in which the number of arguments are declared, and combine the features of the default parameter values, you can choose whether to use the default parameter or not.

Before 4.0, as shown in the following example:

static void Main(string[] args){
OldFun("greenerycn", "cnblogs", "com"); 
}  
static void OldFun( string param1,string param2,string param3){
var content = string.Format("http://{0}.{1}.{2}", param1, param2, param3);
Console.WriteLine(content); 
} 

Previously, the oldfun function must be called according to the benefits of param1, param2, and param3.

Param2 cannot be placed in the first call. You cannot assign values to param2 only, but not to other parameters ..

After 4.0, we can call:

static void Main(string[] args) {
OldFun( "com", param3:"greenerycn", param2:"cnblogs");
}
Result After execution

 

Note: The call method of the named parameter is: Parameter Name: parameter value, and the named parameter must be placed behind the value of a common parameter, that is, it cannot be called in the following way:

Combined with default parameters

This is the maximum purpose of the name parameter. refer to the following code. It is easy to call!

static void Main(string[] args) {
OldFun(param2:"cnblogs"); 
}
static void OldFun( string param1="www", string param2="greenerycn",string param3="com") {
var content = string.Format("http://{0}.{1}.{2}", param1, param2, param3); 
Console.WriteLine(content); 
}

After execution

Call Sequence of parameters

Before naming parameters appear, function parameters are called from left to right. After naming parameters are available, have the function call sequence been changed?

Let's take a look at an example:

static void Main(string[] args) {
TestFun(P1(), P2(), P3()); 
static int P1() {Console.WriteLine("P1");return 1; }
static int P2() {Console.WriteLine("P2");return 2; }
static int P3() {Console.WriteLine("P3");return 3; }
static void TestFun(int a, int b, int c) {
Console.WriteLine(string.Format("a:{0},b:{1},c:{2}", a, b, c));
}

As shown in the preceding code, the execution result is as follows:

P1
P2
P3
A: 1, B: 2, C: 3

We can see that the execution order of the parameters is P1 → P2 → P3.

After using the named parameter?

static void Main(string[] args) { TestFun(P1(),b:P3(), c: P2()); }

The execution result is as follows:

P3
P2
P1
A: 1, B: 3, C: 2

The order is changed to: P3 → P2 → p1

Why?

That's becauseThe compiler first calls the parameter that uses the named parameter, and then executes the remaining parameter.

Poor

There are good places and bad places. A new problem that arises after naming parameters are introduced is:If the parameter name is modified, the caller must also modify the name.

As follows:

static void Main(string[] args) { OldFun(param2: "cnblogs"); }  
static void OldFun( string param1="www",string paramNew2="greenerycn" ,string param3="com") {
var content = string.Format("http://{0}.{1}.{2}", param1, paramNew2, param3);Console.WriteLine(content); 
}

As shown in the preceding code, if param2 is changed to paramnew2, the caller must modify it. Otherwise, an error is returned.

To change it to this:

static void Main(string[] args) { OldFun(paramNew2: "cnblogs"); }
Therefore, if we call a third-party or public function, we recommend that you do not use the name parameter to call the function. This prevents the caller from modifying the name (such as spelling error ), so our Code cannot be compiled.

C # name parameter []

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.