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

Source: Internet
Author: User

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

// 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 learn the value parameter in C #-reference type.

In the previous article, I learned the value parameter -- value type.

We know: 1. The value parameter creates a copy of the variable. 2. Changes to the value parameter will not affect the value of the variable.

This time, let's take a look at the value parameter-reference type to create a new object.

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 ParametersSimple 8 {9 class Program10 {11 static void Main (string [] args) 12 {13 Student stu = new Student () {Name = "Tom "}; 14 Method (stu); 15 Console. writeLine (stu. name ); 16} 17 18 // The parameter passed in when a static Method is created is a Student parameter 19 // an instance is created in the Method and referenced by the stu parameter, the newly created instance is named "Mark" 20 static void Method (Student stu) 21 {22 stu = new Student () {Name = "Mark"}; 23 Console. writeLine (stu. name); 24} 25} 26 27 class Student28 {29 public string Name {get; set;} 30} 31}

After the program runs, the upper and lower lines in the window are displayed respectively:

----------------------------------------------

Mark

Tom

-----------------------------------------------

You can compile it yourself.

Explanation:

The reference type stores the instance address.

The variable stu outside the method stores the object address. The value in the input method is also the object address. Do you still remember: the value parameter creates a copy of the variable. The copy in the method also stores the address of this object.

After the method is called, the value in the copy is changed, that is, the address previously stored in the copy is changed to a new address, naturally, it points to a new object. Do you still remember that changing the value parameter will not affect the value of the variable?

Therefore, variables outside the method still point to the original object, and the changed copy points to a new object, which does not affect each other.

 

This example has a disadvantage: I changed the names of stu objects inside and outside the method to the same. After the program runs, two identical names are printed on the screen, then we can't tell the difference.

So we need to improve it!

GetHashCode () method
The HashCode of each object is different.
Therefore:
1 namespace ParametersSimple 2 {3 class Program 4 {5 static void Main (string [] args) 6 {7 Student stu = new Student () {Name = "Tom "}; 8 Method (stu); 9 Console. writeLine ("{0}, {1}", stu. getHashCode (), stu. name); 10} 11 12 13 14 static void Method (Student stu) 15 {16 stu = new Student () {Name = "Tom "}; // I changed the name here to the same name as the one outside the Method 17 Console. writeLine ("{0}, {1}", stu. getHashCode (), stu. name); 18} 19} 20 21 class Student22 {23 public string Name {get; set;} 24} 25}

Running result

Have you noticed that the values before the names are different, indicating that they point to different objects respectively, and the copies in the method point to a new object

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

To be Continued!

In the next article, we will work with you to learn the value passing parameter-reference type. We will only operate on objects without creating new objects.

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

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.