C # -- pass value parameter (1 ),

Source: Internet
Author: User

C # -- pass value parameter (1 ),

// My C # is followed by Meng Ge (Liu tiemeng) (my formal teacher), and I learned "C # getting started with languages, I also explained some things I don't know. It's a huge fortune and a rare mentor!

 

This time, we will work with you to learn the value parameters in C #.

Pass a value parameter (also called a value parameter)

Value parameter)

What is a value parameter?

This is a definition found in C # Language Specification 5.0:

Used to pass input parameters. A value parameter is equivalent to a local variable, but its initial value comes from the real parameter passed for this parameter. Modifying a value parameter does not affect the real parameter passed by the parameter.

Value parameters can be optional. by specifying the default value, the corresponding real parameters can be omitted.

 

Pass value parameter -- Value Type

Note: 1. Create a copy of the value parameter 2. Changes to the value parameter will not affect the value of the Variable

Let's look at an example:

1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. text; 5 using System. threading. tasks; 6 7 namespace BlogUse 8 {9 class Program10 {11 static void Main (string [] args) 12 {13 Calculator c = new Calculator (); 14 int y = 1024; 15 c. addOne (y); 16 Console. writeLine (y); 17} 18} 19 20 // prepare a Calculator tool for calculation 21 class Calculator22 {23 public void AddOne (int x) // here, x is the 24 {25 x = x + 1; 26 Console. writeLine (x); 27} 28} 29}

This is the running result

Note that we call the AddOne Method for variable y to add 1 to the stored value, but why is y 1024?

This is because the parameters in the method are copies of the passed variables. What we modify in the method body is only a copy of the y passed in, which does not affect the value of the variable y stored outside the method.

Certificate ---------------------------------------------------------------------------------------------------------------------------------------

To be Continued!

In the next article, we will work with you to learn to pass value parameters-reference types and create new objects.

Certificate ---------------------------------------------------------------------------------------------------------------------------------------

I hope that the majority of users will point out the problem, point out where I have understood the mistake, communicate with each other, and make progress together!

Related Article

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.