Learn more about C #-optional parameters, and default values (chapter 1, 1.3 ),

Source: Internet
Author: User

Learn more about C #-optional parameters, and default values (chapter 1, 1.3 ),
Directory

  • C # null type
  • C # optional parameters and default values
C # null type

In daily life, I believe that everyone cannot leave their cell phones! Haha... If the mobile phone manufacturer has produced a new mobile phone, which is not yet priced, what should we do in C #1?

Common solutions:

 1   public class Product 2     { 3         private string name; 4         private ProductPrice price; 5  6         public Product(string name, ProductPrice price) 7         { 8             this.name = name; 9             this.price = price;10         }11     }12     public class ProductPrice13     {14         public decimal Price;15         public bool HasPrice;16     }

The introduction of the void type in C #2 greatly simplifies the process.

 1 public class Product 2     { 3         public string Name { get; set; } 4         public decimal? Price { get; set; } 5         public Product(string name, decimal? price ) 6         { 7             this.Name = name; 8             this.Price = price; 9         }    10     }
C # optional parameters and default values

In our daily development, the same value is always transmitted for specific parameters when calling a method, while the traditional solution is to overload the method. Let's go back to the Product class mentioned in the previous article. If most of our products do not include the price, but optional parameters are introduced in C #4 to simplify this type of operation.

1 public class Product 2 {3 public string Name {get; set;} 5Public decimal? Price {get; set ;}7 public Product (string name,Decimal? Price = null) 8 {9 this. name = name; 10 this. price = price; 11} 12 public static List <Product> GetSampleProducts () 13 {14 List <Product> list = new List <Product> (); 15 list. add (New Product ("hard-mounted Furong Wang")); 16 list. add (new Product ("fine white sand", 9 m); 17 list. add (new Product ("soft white sand", 5.5 m); 18 return list; 19} 20 public override string ToString () 21 {22 return string. format ("{0 }:{ 1}", Name, Price); 23} 24}

Note:

Related Materials

Null type of C #

C # null type

This article is written here. In the next article, we will continue to learn more about deep understanding of C. Thank you!

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.