Diligence. NET 4.0 (4)

Source: Internet
Author: User
Tags new features thread

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
*/

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.