Jane C # study notes is said parameters

 

In many ways we have to execute mass participation can call, the same way we write a lot of parameters to be receiving a complete logic. Pass parameters to call the method written by someone else, it does not seem to be much attention, just pass on it according to the requirements. But when the way we wrote it myself, how to set the parameters? We need to take anything, this sentence right. But sometimes we often do not need to pass parameters.

Speaking of parameters we will first points about formal and actual parameters.

Parameter, parameter is the parameter that when you create a method declaration brackets. You can be understood as a "model." When you call a method, arguments need to follow this "model" to do comparison, in line to be able to pass inside. That type of arguments and parameters must be consistent, if necessary a plurality of transmission parameters, the order must be consistent.

Argument, is to call the method that you pass in the value of that variable.

In other words, I have provided when creating a shell method, logic algorithms are all inside my method is to use this shell to withstand. If a method is running, it can not get value from me, give me an error, which I quit. So when you need to call me, you have to give me a thing, let others take the time to me, I can give to others. And I had to say this is an apple, you can not make me come out that watermelon. So, I ask you what means, what you have to give. Shell understood as parameter, as it looks (type) with the same arguments. Passed in the argument is that the real thing.

Of course, when you create a method, you can assign a default parameter values, so call the method when you can not pass parameters.

 

Next we understand the next parameter passing.

The value of the parameter

Passed by value, the variable delivery of the content stored in the argument. What deposit transfer, stored value types to pass values ​​to reference types passed by reference. Role: transmission of information.

We generally pass parameters are the default values ​​of mass participation.

 static  void the Main ( String [] args) 
        { 
            int Number = . 1 ; 
            Fun (Number); 

            Console.WriteLine ( " Number = {0} " , Number); // results. 1 = Number
             // Number is int, value type, {number in the stack address: 1}
             // value parameter passing, traditional values, pass 1
             // i.e. Fun (number) with Fun (1) there is no difference, then such a look Fun (1) with the number on does not matter,
             // so Fun () how to operate this value related to internal number does not matter. 

            The Console.ReadKey (); 
        } 
        static   Private  void Fun ( int Parameter)
        { 
            // Parameter =. 1 
            Parameter = 2 ; // {Parameter Address: 1} ==> {parameter Address: 2} was changed Parameter 
        }
PS: Type value on the stack is also the address: Address {:} value, a reference type on the stack: {Address: the address of the heap data}.

Reference parameters

Pass by reference, variable parameters passed its own memory address. When you create a method and call the method, you need to add the ref keyword in front of the parameter. Role: Change Data

static  void the Main ( String [] args) 
        { 
            int Number = . 1 ; 
            Fun ( REF Number); // reference parameter passing must be added REF 


            Console.WriteLine ( " Number = {0} " , Number); // result number = 2
             // the PS: type value on the stack is also the address: address {:} value, a reference type on the stack: {address: the address of the heap data}.
            // number is int, value type, the stack address {number: 1}
             // reference parameter passing, passed by reference, the address number is transmitted
             // Fun (address number) with Fun (1) there must be a difference, number address is not equal to 1
             // then this Fun (number address) with the number can visually see the relationship
            
            The Console.ReadKey (); 
        } 
        static   Private  void Fun ( REF  int Parameter) 
        { 
            // internal method modified reference parameters, essentially modifying the argument variables.
            // Parameter moment is Number
             // Parameter =. 1 
            Parameter = 2 ; // {Number Address: 1} ==> {number Address: 2} was changed Number 
        }

Output parameters

Also passed by reference, variable parameters passed its own memory address. When you create a method and call the method, you need to add keywords out in front of the parameter. Role: Back to Results

 

 static  void the Main ( String [] args) 
        { 
            int Number; // output parameters may not be transmitted before the assigned 
            Fun ( OUT Number); // transmission output parameters must be added OUT 


            Console.WriteLine ( " Number = {0} " , Number ); // result = 2 Number 
           
            
            the Console.ReadKey (); 
        } 
        static   Private  void Fun ( OUT  int parameter) 
        { 
            // with reference to the difference parameters: 1, the internal parameter assignment method must be output, output parameters may not be transmitted before assignment
             // the Parameter moment is {number address:} 

            the Parameter= 2 ; // {Number Address:} ==> {number Address: 2} had no value assigned a 2 
        }

 If there is need to return a plurality of result, output parameters may be used.

 

Optimization parameter passing

Sometimes we can not have parameters. This simplifies our code, make the code look cleaner and clearer.

 static  void the Main ( String [] args) 
        { 
            int [] = numberArray new new  int [ 10 ]; 
            numberArray = Fun (numberArray); // call assignment method
             // numberArray array is a reference type, a reference type on the stack: {numberArray Address : numberArray heap address data}
             // this is the value of the transmission, the transmission is: numberArray heap address data 
        }
         ///  <Summary> 
        /// for array assignment
         ///  </ Summary> 
        ///  <param name = "array"> the target array </ param> 
        ///  <Returns> target array </ Returns> 
        static Private  int [] Fun ( int [] Array) 
        { 
            // Array {array address in the stack: numberArray heap address data}
             // numberArray address data in the stack ==> Data. 1 
            for ( int I = 0 ; I <array.length; I ++ ) 
            { 
                Array [I] = I; 
            } 

            // end I just changed data, data 1 data becomes 2: numberArray address data in the stack ==> 2 data, 

            // while the stack array {array address: numberArray heap address data}, without any change.
            // and eventually we need to return a value numberArray address of the heap with data passed in exactly the same. 

            // That is, the method does not require numberArray internal operating data in the address of the heap. To operate on those data points to this address.
            //It is not necessary to me, but they let me get to the line. 
            return Array; 
        }

Just numberArray declared as a global variable, then all methods do not need to pass through arbitrary parameters to get. Because numberArray is an array, and the array is a reference type. So we do not care to change the method in which the reference (address) data on the heap, then a reference (address) through the same, with all other methods of acquiring an already changed data.

Therefore, the above code can be changed

       static   int [] numberArray;
         static  void the Main ( String [] args) 
        { 

            // Fun (); This modulation method where an error occurs, because when the stack is numberArray {numberArray Address:}, no data stack address space assigned to it could not find 
            numberArray = new new  int [ 10 ]; // {numberArray address:} ==> {numberArray address: the address of the heap data}, giving it an open space on the heap. And heap address space assigned to the variable 
            Fun (); // time you tune to this method, numberArray the stack becomes: {numberArray Address: the address of the heap data} 

        } 
        ///  <Summary> 
        / // for array assignment
         ///  </ Summary> 
      
        static  Private  void Fun () 
        {
            // find the address of data stack numberArray piece heap space to store the data into it 
            for ( int I = 0 ; I <numberArray.Length; I ++ ) 
            { 
                numberArray [I] = I; 
            } 

        }

 

Guess you like

Origin www.cnblogs.com/xwzLoveCshap/p/11588197.html