c#--value parameter (1)

Source: Internet
Author: User

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)

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.