C # Value types and reference types

Source: Internet
Author: User

Value types and reference types in layman's terms: there's a different place to store,

Value type int double bool char struct (struct) enum (enum) is stored on top of the managed heap,

Reference type: The String object class is stored on the stack.

In simple terms, such as int a=1; int b=a; When you assign a value to B, you copy the value of a, and you create an area in the center of the memory to save the value of B. The storage of A and B is independent

Class A=new class (); class B=a; When assigning a value to B, simply copy the reference address of A to B. So that A and B point to the same address in memory. A and B are associated.

A value type is a reciprocal assignment between an object and an object

A reference type assigns a pass that has no value for the reference address

Class Program
{

        public class staff
        {
           //reference type
             public string FirstName {get; set;}
            public string LastName {get; set;}
            public int Salary {get; set;}
       }

public struct Employee
{
public string FirstName {get; set;}
public string LastName {get; set;}
public int Salary {get; set;}
}

}

static void Main (string[] args)
{

Staff S1 = new Staff {FirstName = "123"};
Staff s2 = S1; Assignment-only addresses are not assigned a value
S2. FirstName = "ABC";
Employee E1 = New Employee {FirstName = "123"};//e1= "123"
Employee e2 = E1;
E2. FirstName = "ABC";
Console.WriteLine ("s1=" + S1. FirstName + ", e1=" + E1. FirstName);

}

Final output S1=ABC e1=123

C # Value types and reference types

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.