C # 4.0 named and optional parameters for new features, dynamic bindings (dynamics), generic covariance and contravariance, Countdownevent, Barrier
Introduced
New features of C # 4.0
* Named and Optional Arguments-named parameters and optional parameters
* Dynamic Binding-Dynamically binding (dynamic is used dynamically for programming, it relies on the dynamics Language Runtime)
* Covariance-generic covariance
* contravariance-generic type of inversion
* Countdownevent-Thread, task synchronization class. The thread or task blocks until the countdownevent count is 0
* Barrier-Thread, task synchronization class. It is used to synchronize all threads or tasks in a thread group or task group, and the first thread or task that arrives is blocked here
Example
1. Demo of named parameters and optional parameters
NamedAndOptionalArguments.aspx.cs
Code
/*
* Named parameters and optional parameters
* Named parameter: When calling a method, you can pass the argument without a position, but specify the name of the parameter to pass the value
* Optional parameter: When declaring a parameter in a method, you can set a default value for it, the optional parameter can be ignored when the method is called
*/
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Namespace CSharp
{
public partial class NamedAndOptionalArguments:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
Write ("Hello");
Write ("Hello", "WEBABCD");
Write ("Hello", P3:false, P2: "WEBABCD");
}
private void Write (String p1, string p2 = "P2", bool P3 = TRUE)
{
Response.Write (String. Format ("p1:{0}; P2:{1}; P3:{2} ", p1, P2, p3. ToString ()));
Response.Write ("<br/>");
}
}
}
/*
Run Result:
P1:hello; P2:P2; P3:true
P1:hello; P2:WEBABCD; P3:true
P1:hello; P2:WEBABCD; P3:false
*/