Similarities and differences between C # value types and reference types

Source: Internet
Author: User

Int,bool,decimal, etc. as a value type

List,stream, etc. as a reference type

When you set a value type variable equal to another variable with an equal sign, the copy is completed, and then the two variables have no effect on each other;

When you use the equals sign for a reference, the two references point to the same object

For value types and reference types, variable declarations and replication are the same.

int howmany = 25;

bool Scary = true;

List<double> temperatures = new list<double> ();

int and bool are value types, and list is reference type, and they all initialize in the same way

But once the assignment starts, it's different.

Value type assignment:

int fifteenmore = Howmany;

Fifteenmore + = 15;

Console.WriteLine ("Howmany has {0},fifteenmore have {1}", Howmany,fifteenmore);

Output: Howmany has 25,fifteenmore have 40

Reference type Assignment:

Temperatures. ADD (56.5D);

Temperatures. ADD (27.4D);

List<double> differentlist = temperatures;

Differentlist.add (62.9D);

Cosole.writeline ("Temperature has {0},differentlist have {1}", temperature. Count (), Differentlist.count ());

Output: Temperatures has 3,differentlist have 3

Similarities and differences between 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.