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.