C # Advanced Programming reading Note One (about value types and reference types)

Source: Internet
Author: User
Tags bool variables readline reference tostring
Notes | programming | The storage location of advanced data in memory, depending on its data type, is divided into value types and reference types in C #, and value type data is stored in an in-memory stack, each variable or program has its own stack, and a stack address cannot be shared. When a variable of a value type is passed to another variable of the same type, two different addresses are allocated on the stack.

Data of a reference type is stored in an in-memory heap, and the data in the same location can be used by different variables or programs. When data is passed from a variable of a reference type to another variable of the same type, only the reference address of the variable is passed to the new variable, referencing the data stored in the current heap.

You can get a detailed conclusion by example:
Using System;
Defines a rectangular class in which the class belongs to a reference type
Class Refrectangle
{
public int width;
public int height;
}
Defines a rectangular structure that belongs to a value type
struct Valrectangle
{
public int width;
public int height;
}

Class Refvalrectangle
{
public static void Main ()
{
Creates a rectangular object and passes the value to another new object.
Refrectangle ref1 = new Refrectangle ();;
Ref1.width = 3;
Ref1.height = 4;
Refrectangle ref3 = ref1;
Console.WriteLine ("Dimensions of REF1 are:" + ref3.width.ToString () + "..." + ref3.height.ToString ());
Console.WriteLine ("Change Dimensions of Ref1");
Ref1.width = 10;
Ref1.height = 50;
BOOL Btransfer = REF3. Equals (REF1);
Console.WriteLine ("Dimensions of REF1 now are:" + ref3.width.ToString () + "..." + ref3.height.ToString ());
Console.WriteLine (Btransfer.tostring ());
Console.ReadLine ();
Creates a rectangular structure that passes values to a new rectangular structure
Valrectangle val1 = new Valrectangle ();
Val1.width = 3;
Val1.height = 4;
Valrectangle val3 = val1;
Console.WriteLine ("Dimensions of Val1 are:" + val3.width.ToString () + "..." + val3.height.ToString ());
Console.WriteLine ("Change Dimensions of Val1");
Val1.width = 10;
Val1.height = 50;
BOOL Bpass = Val3. Equals (VAL1);
Console.WriteLine ("Dimensions of Val1 now are:" + val3.width.ToString () + "..." + val3.height.ToString ());
Console.WriteLine (Bpass.tostring ());
Console.ReadLine ();
}
}

As you can see, when a variable of the value type is passed, changing the first variable does not affect the second variable, because, when the variable is passed, it is assigned an address to the new variable in the stack, so the two variables are no longer related after the transfer occurs.
When a variable of a reference type is passed, the first one is changed, and the second variable is changed because two variables refer to the contents of one address in the heap, and when a variable changes, the corresponding and in-memory heap changes, and the other variable changes.














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.