Parameters and parameter Modifiers

Source: Internet
Author: User
Using system; using system. collections. generic; using system. LINQ; using system. text; namespace argument {// <summary> // C # by default, real parameters are passed as values. // to pass Parameters as references, C # provide the ref parameter modifier // The parameter can be passed to the function as a value or reference, without considering whether it is a value type or a reference type ///out modifier, similar to ref, but 1. You do not need to assign an initial value when calling a function. 2. You must assign a value before the function is released. // Params modifier is generally used to modify the last parameter, so that it can accept any number of specific types of parameters // Variable Parameter Function in C language, void fun (parm_list ,...); void fun (...); the type can be different. // </Summary> class program {public void Foo (ref int X) {x = x + 1; system. console. writeline ("in Ref foo" + x);} public void Foo (int x) {x = x + 1; system. console. writeline ("in foo" + x);} public void Foo (stringbuilder SB) {sb. append ("ASDF"); system. console. writeline ("in foo" + Sb);} public void split (string name, out string firstname, out string lastname) {int I = Name. lastindexof (''); firstname = Name. substring (0, I); lastname = Name. substring (I + 1);} public int sum (Params int [] ints) {int sum = 0; For (INT I = 0; I <ints. length; I ++) sum + = ints [I]; return sum;} static void main (string [] ARGs) {program P = new program (); int x = 9; p. foo (x); system. console. writeline ("Out foo" + x); p. foo (ref X); system. console. writeline ("out ref foo" + x); stringbuilder STR = new stringbuilder ("Zhang"); p. foo (STR); system. console. writeline ("Out foo" + Str); string a, B; p. split ("Stevie Ray Stewart", out a, out B); system. console. writeline (a); system. console. writeline (B); // Params system. console. writeline (P. sum (1, 2, 3, 4, 5); system. console. writeline (P. sum (New int [] {1, 2, 3, 4, 5 }));}}}

In function overloading, the return value type and Params modifier are not part of the method signature.

The ref and out modifiers can be used as method signatures.

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.