C # also allows default function parameters

Source: Internet
Author: User

As a developer from C ++, C # cannot use default parameters, in this case, the functions that can be implemented by writing a function must be divided into multiple overload functions. But today we seeCodeAnd suddenly found that C # can also use the default parameter, the usage is very similar to C ++.

 
Static VoidTestfun (IntA =10) {Console. Write (A. tostring ());}Static VoidMain (String[] ARGs) {testfun ();}

It's so strange. I can't remember it. If I want to do it, I should have used it for a long time? At first I thought it was a new function of. Net 4.0, but compiled a. NET Framework 2.0ProgramTest. So I asked my colleague to try it with vs2005 and found that the compilation was not successful. net, but the compiler, because what function is called and what parameters are passed are actually decided during the compilation period. Considering that vs2008 was used when C # was first used, after vs2010, it will be supported (it may also be the new feature of C #, OK. In short, it is now supported ).

and I found that this default parameter is more powerful than C ++, for example:

  static   void  testfun ( int  A,  int  B =  10 ,  int  C =  20  ) {console. write (A  +  " "  + B +  " "  +  C) ;}  static   void  main ( string   [] ARGs) {testfun (  3 , C:  30  ) ;} 

This is equivalent to setting the C value, while the B value uses the default value. In C ++, you must specify the C value, i'm afraid you must also specify the value of B.

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.