Parameters with default values in C # methods

Source: Internet
Author: User

When designing parameters for a method, you can assign default values for some or all of the parameters. The code that calls these methods can then choose not to specify partial arguments and accept their default values. In addition, when you invoke a method, you can also pass an argument to it by specifying the name of the parameter. The following code demonstrates the use of optional parameters and named parameters:

usingSystem;usingSystem.Collections.Generic;usingSystem.Text;namespaceparameterinmethod{classProgram {Private Static intS_n =0; Private Static voidMintx =9,strings ="A", DateTime dt =default(DateTime), GUID guid =NewGuid ()) {Console.WriteLine ("X={0},s={1},dt={2},guid={3}", X,s,dt,guid); }        Static voidMain (string[] args) {            //1. Equivalent to M (9, "A", Default (DateTime), New Guid ());M (); //2. Equivalent to M (8, "X", Default (DateTime), New Guid ());M8,"X"); //3. Equivalent to M (5, "A", DateTime.Now, New Guid ());M5, Guid:Guid.NewGuid (), Dt:DateTime.Now); //4. Equivalent to M (0, "1", Default (DateTime), New Guid ());M (s_n++, s_n++.            ToString ()); //5. Equivalent to the following two lines of code://string T1 = "2"; int t2 = 3; //M (t2, T1, default (DateTime), New Guid ());M (S: (s_n++). ToString (), x:s_n++);            Console.ReadLine (); //The program output results are as follows:            /** X=9,S=A,DT=0001/1/1 0:00:00,guid=00000000-0000-0000-0000-000000000000 * X=8,S=X,DT=0001/1/1 0:00:00,guid=00000000-0000-0000-0000-000000000000 * X=5,s=a,dt=2012/5/31 10:34:28,guid=cdc348c3-6435-40ed-8e3 7-23D90F4F05CE * X=0,S=1,DT=0001/1/1 0:00:00,guid=00000000-0000-0000-0000-000000000000 * x=3,s=2,d T=0001/1/1 0:00:00,guid=00000000-0000-0000-0000-000000000000*/        }    }}
View Code

If an argument is omitted when called, the C # compiler automatically embeds the default value of the parameter.
In the 4th call to M, the current value in S_n (0) is passed to X, and then s_n increments. Subsequently, the current value of S_n (1) is passed as a string to S, and then continues to increment to 2. When passing arguments with named parameters, the compiler still evaluates the arguments in left-to-right order.
In the 5th call to M, the current value in S_n (2) is converted to a string and saved to a temporary variable (t1) created by the compiler. Next, S_n increments to 3, which is saved to the compiler to create another temporary variable (T2). Then, S_n continues to increment to 4. When you finally call M, the argument passed to it is t2,t1 a default datetime and a new GUID.

Rules and principles:

You can specify default values for methods, constructor methods, and parameters that have parameter properties (C # indexers). You can also specify a default value for the parameter that is part of the delegate definition. Then, when you call a variable of the delegate type, you can omit the argument to accept the default value.
A parameter with a default value must be after all parameters that do not have a default value. In other words, once a parameter with a default value is defined, all arguments to the right of it must also have a default value. For example, in the M method definition in front of you, if you delete the default value of S ("A"), a compilation error occurs. There is one exception to this rule: The parameter array must be placed after all parameters (including those with default values), and the array itself cannot have a default value.
The default value must be a constant value that can be determined at compile time. So, what parameters can be set for default values? The types of these parameters can be the primitive types that C # determines. You can also include enumeration types, as well as any reference types that can be set to null. For a parameter of any value type, you can set the default value to an instance of a value type and have all its fields contain 0 values. You can use the default keyword or the new keyword to express the meaning, and the two syntaxes will produce exactly the same IL code. When you set the default values for the DT parameter and the GUID parameter in the M method, the two syntaxes are used respectively.
Note Do not rename parameter variables. Otherwise, any caller must modify their code if they pass arguments in the form of a parameter name. For example, in the previous M method declaration, if you rename the dt variable to dateTime, the third call to M will cause a compilation error: Error CS1739: The best overload of "M" does not have a parameter named "DT".
If the parameter is identified with a ref or out keyword, the default value cannot be set. Because there is no way to pass a meaningful default value for these parameters.
C # does not allow the omitting of arguments between commas, such as M (1,, DateTime.Now). Because of the effect of readability, programmers will be forced to count these commas.

Parameters with default values in C # methods

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.