C # Basic Learning (0724) variable parameters, ref and out

Source: Internet
Author: User

Params variable parameter, no matter how many parameters, must appear at the end of the parameter list, you can pass an array of the corresponding type for the variable parameter directly

  #region  variable parameter // 1. If the method has more than one argument, the mutable argument can be the last argument  // 2. A variable parameter can pass a parameter or not pass a parameter, and if you do not pass a parameter, the args array is an array of length 0  // 3. Variable parameters can be passed directly in an array  static  void  Test (string  msg,params  int  [] args) { //  if  (Args!=null  ) {}} 
#endregion

static void Main (string[] args )
{

Console.WriteLine ("Hello world!");
Test ("AA", 10, 1, 2, 32, 4, 5, 6);
int[] Arrint = new int[] {1, 2, 3, 4, 5};
Test ("xxx");
Test ("AAAA", arrint);
Test ("AAAA", null);
Console.readkey ();
Console.WriteLine ("");
list<string> list = new list<string> ();
Console.WriteLine ("===={0}====", list);
Console.readkey ();

}

How to use ref parameters

//ref is just an address, a reference pass, you can force a value pass to a reference pass//Ref: The parameter must be assigned before it is passed, and in the method it can be used without assigning a value to the ref parameter, directly using the//Ref: External values are changed inside the scenario, out is internally assigned to external variables, out is generally used in places where functions have multiple return values#regionRef parameterStatic voidJianjin (ref intm) {m=m+ -;}Static voidKoukuan (ref intm) {m=m- -;}#endregion//UseStatic voidMain (string[] args) {  intmoney= +; Jianjin (refMoney ); Koukuan (refMoney );  Console.WriteLine (Money); Console.readkey ();}

How to use out parameters

#regionOut parametersStatic voidTest1 ( out intx) {  //The out parameter must be assigned a value in the method before it is used//The out parameter cannot get the value in the passed in variable, only the variable passed in is assigned a value//the out parameter must be assigned before the method finishes executing. //Console.WriteLine (x);//Error: An unassigned out parameter "x" was usedx= -; X++;}#endregion//Out UseStatic voidMain (string[] args) {  intm= +;//The out method can not pass the value, it is useless to pass, because the method must be assigned a value for the out parameterTest1 ( outm); Console.WriteLine (m);}

Out multiple parameter usage scenarios

//MethodStatic intGetage ( out stringName out intheight) {Name="Super Brother"; Height= the; return  +;}//using a scenario, you can also return multiple values using refStatic voidMain (string[] args) {  //two parameters can be declared inline  stringN; inth; intAge=getage ( outN outh);  Console.wreteline (age);  Console.WriteLine (n);  Console.wreteline (h);  Console.readkey (); #regionOut Use Scenario 2strings="ABC"; intresult; //converts the string representation of a number to his equivalent of 32 as a signed integer. A return value that indicates whether the conversion succeeded  BOOLb=int. TryParse (s), outresult); if(b) {Console.WriteLine ("The conversion was successful, the result is: {0} ", result); }  Else{Console.WriteLine ("conversion failed with the result: {0}", s)} #endregion}

Ref Exchange two variable positions

#regionSwap two variablesStatic voidSwap (ref intN1,ref intn2) {  inttmp=N1; N1=N2; N2=tmp;}#endregionStatic voidMain (string[] args) {  intm=Ten, x= -; Swap (refMrefx);  Console.WriteLine (m);  Console.WriteLine (x); Console.readkey ();}

Out User Login Exercise

Private Static BOOLValiduserlogin (stringUidstringPwd out stringmsg) {  BOOLisok=false; if(uid!="Admin") {msg="User name Error"; }  Else if(pwd!="888888") {msg="Password Error"; }  Else{isOk=true; Msg="Welcome User:"+USD; }  returnisOk;}//UseStatic voidMain (string[] args) {#region用户登录练习 while(true) {Console.WriteLine ("Please enter user name:"); stringUid=Console.ReadLine (); Console.WriteLine ("Please enter your password:"); stringPwd=Console.ReadLine (); stringMsg//can be declared inline    BOOLIsok=validuserlogin (Uid,pwd, outmsg); if(isOk) {Console.WriteLine ("Login Successful ===={0}", MSG); }    Else{Console.WriteLine ("Login failed ===={0}", MSG); }  }  #endregion}

C # Basic Learning (0724) variable parameters, ref and out

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.