C # -- pass the value parameter (3 ),

Source: Internet
Author: User

C # -- pass the value parameter (3 ),

In the previous article, I learned the value parameter-reference type.
This time, we will work with you to learn to pass the value parameter-reference type. We will only operate on objects without creating new objects.
This is a mind map:

We still need to remember: 1. The value parameter creates a copy of the variable. 2. Changes to the value parameter do not affect the value of the variable.
Let's look at an example:

1 class Program 2 {3 static void Main (string [] args) 4 {5 Student stu = new Student () {Name = "Elliot"}; 6 Console. writeLine ("Name is {0}, HashCode is {1}", stu. name, stu. getHashCode (); 7 UpdateObject (stu); 8 // call the method and print it again. writeLine ("Name is {0}, HashCode is {1}", stu. name, stu. getHashCode (); 10 11} 12 13 // no new object 14 static void UpdateObject (Student stu) 15 {16 stu. name = "Mark"; 17 Console. writeLine ("Name is {0}, HashCode is {1}", stu. name, stu. getHashCode (); 18} 19} 20 21 class Student22 {23 public string Name {get; set;} 24} 25}

 


Running result:


See:
The first line is to create an object in the Main method and print out its name and hashcode immediately;
The second line is printed in the UpdateObject method;
The third line is printed again after the method is called.
The last two rows are exactly the same.
Explanation:
Variables of the reference type store the address of the object in the heap memory, and the passed parameters are copies of the variable. Therefore, they all store the address of the object, we access the object through parameters and modify the values in the object. Because the variables and parameters both point to the same object, their names become the same, while hashcode remains the same.

The object is still the object, but the value in the object has changed.
This means that we use the value parameter to update only the object without creating a new object.
Note:
In practical work, it is rare to change the value of an object by passing in parameters like this, because the main output of a method depends on the return value, we call this operation to change the value of an object through the passed parameter side-effects of a method. We should avoid this side-effects whenever possible during work.

Bytes ------------------------------------------------------------------------------------------------

To be continued!

In the next article, we will work with you to learn the reference parameters.

Bytes ------------------------------------------------------------------------------------------------

I hope you will make comments, point out the problem, share the exchange, 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.