. NET C # parameter reference ref pass in value type

Source: Internet
Author: User


The following code is attached:

The code is as follows Copy Code
Using System;
Using System.Collections.Generic;
Using System.Text;

Namespace Classobjectandstringdiffdemo
{
Class Program
{
static void Main (string[] args)
{
As the variable or object passed in as an argument, the following is called ' original variable ' or ' original object '
String str = "123";//"123" is an object, which is equivalent to string str = new String ("123"); the process of assigning a value has actually changed the memory address point

Console.WriteLine ("/********************************/");
Console.WriteLine ("1: All passes are the copy of the object value of the passing type,");
Console.WriteLine ("A copy of the value type is a value, a copy of the reference type is the reference address of the object)";
Console.WriteLine ("2:string definition type is class, reference type, other basic numeric type, struct, so value type,");
Console.WriteLine ("So the string's assignment procedure is an object's memory Exchange,");
Console.WriteLine ("String str =" 123;//"123" is an object, equivalent to String str = new String ("123"); ");
Console.WriteLine ("3: Whether the type of the argument is a reference type or a value type, can be called by value);"
Console.WriteLine ("Because, pass is copy, value type is value copy, reference type is copy of reference address");
Console.WriteLine ("4: Inside the parameter function, the operation is copy, the value type is manipulated as a copy of the incoming variable (value) (New memory address)");
Console.WriteLine ("So the change is the copy (the new memory address), independent of the original variable, the operation is not pointing to the same block of memory;");
Console.WriteLine ("Reference type is the copy of the address value of the variable (object) passed in, although it is a copy but still points to the memory address of the original variable");
Console.WriteLine ("So the copy and the original variable point to the same memory address, the copy operation is the contents of the memory address of the original variable.") ");
Console.WriteLine (The object of the 5:string and custom class is the same as the incoming argument, but the result of the operation is different because of a particular particularity of the string as it is defined, ");
Console.WriteLine ("That is, declaring initialization differs from regular classes, does not require constructors to be constructed, so the difference in performance hides its essence from being in the same reference");
Console.WriteLine ("The same attribute of the general class of the type, the assignment process of string, that is, the memory address of a string object is given an incoming copy copy");
Console.WriteLine ("That is, the same as the person class object instance, the replica PS1, the new object ps2,ps1=ps2, so that after the operation of the PS1 object field, in fact, the operation of the PS1 is not the original copy,");
Console.WriteLine ("is not the copy memory address of the original object, but the new PS2 memory address, so that it does not work");
Console.WriteLine ("/********************************/");

Console.WriteLine ("Str initialization value");
Console.WriteLine ("Str:{0}", STR);

Changestring (str);
Console.WriteLine ("Str is passed with no modified parameters, passed by address (reference)");
Console.WriteLine ("Str:{0}", STR);

Changestring (ref str);
Console.WriteLine ("Str-modified pass with ref, passing by address (reference)");
Console.WriteLine ("Str:{0}", STR);

person PS = new person ("111");

Console.WriteLine ("Person initializes name value");
Console.WriteLine ("Person.name:{0}", Ps.name);

Changepersonnew (PS);
Console.WriteLine ("Person object is passed without modification parameter, be passed by address (reference), change value way is different");
Console.WriteLine ("Person.name:{0}", Ps.name);

Changepersonnew (ref PS);
Console.WriteLine ("Person object is passed without modification parameter, be passed by address (reference), change value way is different");
Console.WriteLine ("Person.name:{0}", Ps.name);

CHANGEPERSONNEW2 (ref PS);
Console.WriteLine ("Person object is passed without modification parameter, be passed by address (reference), change value way is different");
Console.WriteLine ("Person.name:{0}", Ps.name);

Changeperson (PS);
Console.WriteLine ("Person object is passed without modification parameter, is passed by address (reference), change value way is different)";
Console.WriteLine ("Person.name:{0}", Ps.name);


int k = 1;

Console.WriteLine ("K Initialization value");
Console.WriteLine ("K:{0}", K);

Changeval (k);
Console.WriteLine ("K is passed with modified parameters, passed by value");
Console.WriteLine ("K:{0}", K);

Changeval (ref k);
Console.WriteLine ("K with ref modifier parameter pass, pass by reference");
Console.WriteLine ("K:{0}", K);

Console.ReadLine ();
}
<summary>
///
</summary>
<param name= "s" > Reference type, ref acts as a pointer, so the address of the original variable is passed in, and the original variable must be initialized first </param>
static void Changestring (ref string s)
{
Ref functions, the individual based on the results of guessing, should be kept s copy of the memory address reference in the assignment process does not change, only change the value
s = "465";//s the memory address of the original variable at this point, but the value changes, the address of the replica points to the memory of the original variable, and the value changes, then change the original variable's point to the memory content

This situation is equivalent to the following Changeperson (person p)
}

<summary>
///
</summary>
<param name= "s" > Reference type, the memory address of the original variable is passed in here </param>
static void Changestring (string s)
{
s = "456";//The memory address of the original variable when the copy of S is passed in, at which point the memory address of ' 456 ' is pointed to, the memory point of the replica is changed, and the memory content of the original variable is not affected.

This situation is equivalent to the following changepersonnew (person Pson)

This is the equivalent of string assignment, hiding invisible code
String str = new String ("456");
Suppose the string class has a field, and ' 456 ' is equivalent to the value of that field equivalent to the String class
The S=str;s object points to the Str object. Only changes to the replica that do not involve changes to the original object
}
Because the string type is special, it's easier to see the difference by looking at the following value types

static void Changeval (int i)
{
i = 2;//Here I passed a copy of the value of the original variable (new memory address, saved the value of the original variable), so change the value of copy I, does not affect the contents of the original variable memory
So the result has no change
}

static void Changeval (ref int i)
{
i = 3;//a copy of the memory address of the original variable passed here (the new memory address, the memory address of the saved original variable, that is, the new memory address pointing to the memory address of the original variable)
At this point, modify the value, that is, the value of the saved contents in the memory of the original variable, so the result will change.
}


<summary>
///
</summary>
<param name= "P" > reference type, a copy of the memory address passed in to the original object </param>
static void Changeperson (person p)
{
P.name = "n"; the memory point of the//name field to the memory pointing to ' 000 ', but P's memory points to the unchanged (the original object's address), so p.name points to the ' 000 ' memory address
So the result of the performance is the value changed, in fact, the name point to the memory is not the original block of memory
}
<summary>
///
</summary>
<param name= "P" > reference type, a copy of the memory address passed in to the original object </param>
static void Changepersonnew (person Pson)
{
Person PSO = new person ();
Pso.name = "222";
Pson = Pso;//pson The memory point of the replica has been changed to the PSO's memory address, so it does not affect the contents of the original variable in memory
}

<summary>
///
</summary>
<param name= "P" > reference type, a copy of the memory address passed in to the original object </param>
static void Changepersonnew (ref person Pson)
{
Person PSO = new person ();
Pso.name = "333";
Pson = Pso;//ref has the function of keeping the memory address unchanged, changing only the value, and the object includes the field
}

<summary>
///
</summary>
<param name= "P" > reference type, a copy of the memory address passed in to the original object </param>
static void ChangePerSonNew2 (ref person Pson)
{
Person PSO = new person ();
Pso.name = "444";
Pson.name = pso.name;//
Pson = Pso;//ref has the function of keeping the memory address unchanged, changing only the value
}

Sealed class Person
{
public string name;
public person (string n)
{
THIS.name = n;
}
Public person ()
{ }
}
}
}


Everyone after reading to give some advice, help me to amend, of course, if there is more easily understandable, and professional, please share ha, compare this article in the individual

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.