C #4.0 new features: optional parameters, named parameters, Dynamic

Source: Internet
Author: User

1. Optional parameters

You can set a default value for the method parameters, as shown in the following code: class Program {static void Main (string [] args) {Show (); Show ("cary"); Console. readLine ();} public static void Show (string msg = "") {Console. writeLine ("Hello {0}", msg );}} 2. Named ParametersThe Show method above can be called as follows: Show (msg: "cary"); when your method has multiple optional parameters of the same type (optional parameters, named parameters is particularly useful. Without naming parameters, the compiler does not know which parameter is passed. 3. Dynamic FeaturesThe dynamic keyword is used to declare a dynamic object, and then call a method or read/write attribute through the dynamic object. In the past, we used reflection, Emit, CodeDom, and other technologies at runtime. To create a dynamic object, you must use a special builder called ExpandoObject. Class Program {static void Main (string [] args) {dynamic person = new ExpandoObject (); person. name = "cary"; person. age = 25; person. showDescription = new Func <string> () => person. name + person. age); Console. writeLine (person. name + person. age + person. showDescription (); Console. readLine ();}

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.