C #----data type (params/ref/out)

Source: Internet
Author: User

This is a variety of data types, summary of various parameters. It's a little messy. Didn't have time to tidy up. I'll get it sorted out in a few short time. And this is the collection of information and some of their own summary. Thank you all the way to the great God!! 1. Theoretical Concepts  C # Data type structure diagram:

A computer is divided into 5 districts

1. Constant Zone 2. Static zone (statically decorated) 3. Code area is found in code area when you run the method 4. Stack area (value type, define a variable into the stack to open up space, define the automatic free space) int a = 10, Here A is the value 5. The heap area (in the array, new is called the operator, New is created in the heap space) int[] A = new int[]; Here A is the stored address copy value type is copy the same number, and then assigns the value to the new variable reference type is to assign the stored address to the new variable (array) 2. Example(1) Value type conversion reference type
  classMainClass { Public voidJiaohuan (intXinty) {x=x+y; Y=x-y; X=x-y; } MainClass MainClass=NewMainClass (); intA = A; intb = *; MainClass.      Jiaohuan (A, b); Console.WriteLine ("A={0},b={1}", A, b);//However, the arguments are not exchanged      intc = -; intD =C; D= +; Console.WriteLine ("C={0},d={1}", c,d);

Output does not change
If you want to change it, add ref before the parameter of the method, plus ref before the actual parameter of the output; (ref means to convert a value type to a reference type)When the value of the parameter changes, the value of the argument changes as follows:
 classMainClass { Public voidJiaohuan (ref intXref intY//ref applies only to value types, equivalent to converting value types to reference types{x=x+y; Y=x-y; X=x-y; }           Public Static voidMain (string[] args) {MainClass MainClass=NewMainClass (); intA = A; intb = *; MainClass. Jiaohuan (refArefb); Console.WriteLine ("A={0},b={1}", A, b);//when the value of a parameter changes, the value of the argument changes

(2) If the array is a reference type, the output will change
    Public voidMaopao (int[]a) { for(inti =0; i<a.length-1; i++){                     for(intj =0; j<a.length-i-1; j + +){                         if(a[j]>a[j+1])                         {                              inttemp = a[j+1]; A[j+1] =A[j]; A[J]=temp; }                    }               }  int[] s = {1,5,6,7,2,4,3}; int[] h =s; h[1] =Ten; Console.WriteLine (s[1]); MainClass. Maopao (s);

Output Change ~!! Note: Fields in the class are stored in the heap (even if the variables in the field are value types)3. Packing, unpackingBoxing: The process of converting value-type data into reference-type data
         int   at ;          object obj = num;         Console.WriteLine (obj);
Unpacking: Data that converts reference-type data into value types is called unboxing
         int count = (int) obj;         Console.WriteLine (count);
Example:①params represents a mutable array, that is, the length of an indeterminate array. And no additional parameters are allowed after the params keyword.
           Public voidPrintarr (params int[] a)//variable array parameter params          {               foreach(intNincha) {Console.Write (n+" "); }          }
//Output Array int[] sh = { A, Wu, $, +, $}; MainClass. Printarr (SH); Console.WriteLine (); MainClass. Printarr (2,5,7,8,4,9);//omit to define array after using paramsConsole.WriteLine (); MainClass. Printarr ();
② if the params exists at the same time as other value types, then the default value type is a mutable array
           Public voidPrintarr (intAintBparams int[] JK)//variable array parameter params exists with other value types          {               foreach(intYinchJK) {Console.Write (y+" "); }} mainclass. Printarr (2,5,7,8,4,9);//omit to define array after using params
Output: 7 8 4 9 method parameter keywords for C # reference: params, ref, and out if ref or out is not used when declaring a parameter for a method, the parameter can have an associated value. The value can be changed in the method, but the changed value is not preserved when control is passed back to the calling procedure. You can change this behavior by using the method parameter keyword. 4.refThe use of ref is mentioned above in the example introduction of value types and reference types. The REF keyword enables parameters to be passed by reference. The effect is that when control is passed back to the calling method, any changes made to the parameter in the method are reflected in the variable. To use the ref parameter, both the method definition and the calling method must explicitly use the REF keyword. The arguments passed to the ref parameter must be initialized first. This differs from out in that the out parameter does not need to be explicitly initialized before it is passed. property is not a variable and therefore cannot be passed as a ref parameter. Although ref and out are handled differently at run time, they are handled identically at compile time. Therefore, if one method takes a ref parameter and the other method takes an out parameter, the two methods cannot be overloaded. For example, from a compilation point of view, the two methods in the following code are identical. Attempting to do so will result in the code not being compiled. If a method takes a ref or out parameter, and the other method does not take these two types of arguments, it can be overloaded. Example:
Keywords_ref.cs using System; class app{public    static void Useref (ref int i)    {        i + = +; 
       console.writeline ("i = {0}", i);    }     static void Main () {int i = ten;//view value before calling Method Console.WriteLine ("Before-the method calling:i = {0}", i); Useref (ref i);//View the value after the method is called Console.WriteLine ("after that method calling:i = {0}", i); Console.read (); }}
Console output: Before the method calling:i = 10i = 110After the method calling:i = 110 5.outThe Out keyword causes parameters to be passed by reference. This is similar to the REF keyword. differs from REF: REF requires that a variable be initialized before it is passed. Although a variable passed as an out parameter does not need to be initialized before it is passed, the method needs to be called to assign a value before the method returns.
    //How to buy cigarettes +out         Public intMaiyan (intMoney out intChange//The output of the change, if out, is to assign the formal parameter to the argument.        {             intnum = money/ -; Change= money% -; returnnum; }                //An output parameter, when the return value is two, is added out at the method definition parameter ,               intLingqian; intNumber = MainClass. Maiyan ( -, outLingqian);//How much money did you take ?Console.WriteLine (number); Console.WriteLine (Lingqian);
As with the ref example, just change ref to out, and the variable I only needs to be declared.
Static void Main () {    //int i = 10; change to    int  i    ; //}
6. Parametric paramsThe params keyword can specify a method parameter that takes a parameter at a variable number of arguments. No additional arguments are allowed after the params keyword in the method declaration, and only one params keyword is allowed in the method declaration. Array Parameters Summary params

1. The length of the parameter is variable. The length can be 0.

2. Use only once, and put it to the end.

3. Followed by array type, cannot be used together with ref, out  The parameters of a method in C # are of type 4:value parameter: No modifiers are attached. output parameters: declared with the out modifier, you can return one or more values to the caller. Reference parameter: declared with the ref modifier. array Parameters: declared with the params modifier. Example:
//Keywords_params.cs usingSystem;classapp{ Public Static voidUseparams (params Object[] list) {         for(inti =0; I < list. Length; i++) {Console.WriteLine (list[i]); }    }     Static voidMain () {//It is common practice to construct an array of objects and then use this array as a parameter to the method        Object[] arr =New Object[3] { -,'a',"keywords" };         Useparams (arr); //with the params modification method parameter, we can use a set of objects directly as parameters//of course, this set of parameters needs to conform to the calling method of the parameter requirementsUseparams ( -,'a',"keywords");    Console.read (); }}
 

C #----data type (params/ref/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.