Parameter: optional parameter and name parameter.

Source: Internet
Author: User

Parameter: optional parameter and name parameter.

When designing a method parameter, you can assign the default value to some or all parameters. Then, the code that calls these methods can choose not to specify some real parameters and accept their default values.

In addition, you can also specify the parameter name to pass real parameters for the method.

The Demo code is as follows:

Public static class program {private static int sn = 0; private static void M (int x = 9, string s = "A", DateTime dt = default (DateTime ), guid guid = new Guid () {Console. writeLine ("x = {0}, s = {1}, dt = {2}, guid = {3}", x, s, dt, guid );} static void Main (string [] args) {M (); // display all default values M (8, "X"); M (5, guid: Guid. newGuid (), dt: DateTime. now); // guid, dt is the specified parameter name M (sn ++, sn ++. toString (); // here the result is x = 0, s = 1 because the sn ++ first executes and then performs the + 1 operation. After the M method ends, the sn is 2.
  
M (s :( sn ++. ToString (), x: sn ++); Console. ReadKey ();
}
}
// The output result is: // x = 9, s = A, dt = 0001/1/1 Monday 0:00:00, guid = 00000000-0000-0000-0000-000000000000 // x = 8, s = X, dt = 0001/1/1 Monday 0:00:00, guid = 00000000-0000-0000-0000-000000000000 // x = 5, s = A, dt = 8:55:47, guid = 6387f914-cd37-4aa8-a144-2524e4972a44 // x = 0, s = 1, dt = 0001/1/1 Monday 0:00:00, guid = 00000000-0000-0000-0000-000000000000 // x = 3, s = 2, dt = 0001/1/1 Monday 0:00:00, guid = 00000000-0000-0000-0000-000000000000-

If you specify the default value for some parameters in the defined method, pay attention to the following additional rules and principles:

1. You can specify the default value for Parameters of methods, constructor methods, and parameter attributes (c # indexer. You can also specify the default value for parameters that are part of the delegate definition. Then, when you call a variable of the Delegate, You can omit the real parameter to accept the default value.

2. Parameters with default values must be placed after parameters without default values (left side). For example, if the preceding string s does not have the default value, a variant error occurs. Except for "parameter array", this parameter is placed after all parameters, and the array itself cannot have default values.

3. The default value must be a constant value that can be determined during compilation. C # The identified primitive type, enumeration class, and any reference type that can be set to null can be set as the default value. For a parameter of any value type, you can set the default value to an instance of the value type and make it

All fields contain zero values. Use default and new to express this meaning. (Dt, guid)

4. Do not rename the parameter variables. Otherwise, you must modify the code of any call.

5. If the method is called from outside the module, it is potentially risky to change the default value of the parameter.

6. If the parameter is identified by the ref or out keyword, the default value cannot be set, because there is no way to pass a meaningful default value for these parameters.

When you call a method using optional or named parameters, there are also the following additional rules:

1. Real parameters can be passed in any order. However, named real parameters can only be passed at the end of the parameter list.

2. parameters without default values can be passed by name. However, all required arguments must be passed by location or name)

3. Real parameters between commas cannot be omitted in c.

If the parameter requires ref/out, use the following syntax to pass real parameters by passing parameter values:

// Method declaration

Private static void M (ref int x ){...}

// Method call

Int = 5;

M (x: ref );

 

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.