
C# default value parameter examples
If you have used PHP programming, you must be familiar with the parameter default value, which is to initialize a value when defining the method parameter. Now .net4.0 also supports parameter default values. In some cases, it is no longer necessary to initialize with the constructor. This article will take C# as an example to introduce the parameter default value of .net.
I. C# default value parameter example 1
public class UserInfo
{
/// <summary>
/// C# default value paramete
/// </summary>
/// <param name="userName">UserName</param>
/// <param name="sex">Sex</param>
/// <param name="age">Age</param>
/// <param name="addr">Address</param>
public void User(string userName = "Robiter", bool sex = true, int age = 22, string addr = "100 avenue road")
{
HttpContext.Current.Response.Write(string.Format("userName = {0}; sex = {1}; age = {2}; addr = {3}", userName, sex, age, addr));
//Console.WriteLine(string.Format("userName = {0}; sex = {1}; age = {2}; addr = {3}", userName, sex, age,addr));
}
}
Call:
UserInfo ui = new UserInfo();
ui.User();//Output: userName = Robiter; sex = True; age = 22; addr = 100 avenue road
ui.User("Moliner");//Output: userName = Moliner; sex = True; age = 22; addr = 100 avenue road
ui.User("Moliner", false, 23, "200 cloud sky Road");
//Output: userName = Moliner; sex = False; age = 23; addr = 200 cloud sky Road
ui.User(age:25);//Output: userName = Robiter; sex = True; age = 25; addr = 100 avenue road
ui.User(userName:"Moliner",age:25); or ui.User("Moliner",age:25);
//Output: userName = Moliner; sex = True; age = 25; addr = 100 avenue road
ui.User(sex:false, age:23,addr:"200 cloud sky Road");
//Output: userName = Robiter; sex = False; age = 23; addr = 200 cloud sky Road
Input result analysis summary:
If no parameter is provided, the default value of all parameters will be output;
If one parameter is provided, the default is the first parameter, and the others take default values;
If all parameters are provided, there is no need to specify with parameter variables;
If the only parameter provided is not the first parameter, you need to specify it with a variable, such as age:25;
If the provided parameter can be distinguished by .net, it does not need to be specified by parameter variable, such as ui.User("Moliner",age:25), of course, it can also be specified;
II. C# default value parameter example 2
It mainly states that "optional parameters must appear after all required parameters". For example, the string addr is adjusted to the second position in the following method, which is exactly the reason.
public class UserInfo
{
/// <summary>
/// C# default value parameter example 2
/// </summary>
/// <param name="userName">UserName</param>
/// <param name="addr">Address</param>
/// <param name="sex">Sex</param>
/// <param name="age">Age</param>
public void Users(string userName, string addr, bool sex = true, int age = 22)
{
HttpContext.Current.Response.Write(string.Format("userName = {0}; addr = {1}; sex = {2}; age = {3}", userName, addr, sex, age));
}
}
Call:
UserInfo ui = new UserInfo();
ui.Users("Petait", "100 avenue road", false);
//Output: userName = Petait; addr = 100 avenue road; sex = False; age = 22
ui.Users("Petait", "100 avenue road", age:28);
//Output: userName = Petait; addr = 100 avenue road; sex = True; age = 28
The two examples cited above roughly illustrate the default values of the .net parameters. If you want to grasp more comprehensively and apply more freely, you can only learn and summarize in the process of programming.
-
Related Reading
- C# listview select row(a row and multiple rows)
- C# Read and write to text file Newline, with one lin
- C# merge binary array(byte array)
- C# Winform button transparent background with proper
- C# float, double range, precision, definition and ty
- C# split string with \n pay attention to the problem
- Javascript get current url, domain from url, relativ
- C# efficiency comparison of multiple strings concate
- C# date format and examples, with binding and ToStri
- Bubble sort algorithm example in C# and C (Ascending
- C# Hashtable create, add, update, remove and removed
- C# judge whether cookies are disabled on the client