My C # is followed by brother (Liu Tieno) (my formal teacher), "The introduction of C # language," the study, on the elder brother also explained to me some do not know the place, for me is simply a huge wealth, a rare mentor!
This time with everyone. Learn about value parameters in C #
Value parameter (also called value parameter)
Value parameter (parameter)
What is a value parameter?
This is a section of the definition found in C # language Specification 5.0:
Used to pass input parameters. A value parameter is equivalent to a local variable, except that its initial value is derived from the argument passed for that parameter. Modifications to a value parameter do not affect the arguments that are passed for the formal parameter.
The value parameter can be optional, and the corresponding argument can be omitted by specifying a default value.
Pass-value parameter-value type
Note: 1. The value parameter creates a copy of the variable 2. Changes to the value parameter do not affect the value of the variable
Look at an example:
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Threading.Tasks;6 7 namespaceBloguse8 {9 class ProgramTen { One Static voidMain (string[] args) A { -Calculator C =NewCalculator (); - inty =1024x768; the C.addone (y); - Console.WriteLine (y); - } - } + - //Prepare a calculator tool that can be calculated + classCalculator A { at Public voidAddOne (intx) //The x here is the pass-value parameter - { -x = x +1; - Console.WriteLine (x); - } - } in}
This is the result of the operation
Did you notice? We called the AddOne method on the variable y to add 1 to its stored value, but why is y still 1024?
This is because the parameter inside the method is a copy of the variable that is passed in, and what we are modifying in the method body is just a copy of Y passing in, which does not affect the value stored by the method outside the variable y
--------------------------------------------------------------------------------------------------------------- ------------------------
To be continued!
The next chapter to learn about the value of the pass parameter--reference type, create a new object
--------------------------------------------------------------------------------------------------------------- ------------------------
Hope that the majority of netizens point out the problem, point out where I understand the wrong, common exchanges and common progress!
c#--value parameter (1)