C # base (d) (C # predefined value types and reference types)

Source: Internet
Author: User

First, predefined types.

1, value type and reference type

Data types in C # can be classified into value types and reference types, value types are stored on the stack, and reference types are stored on the managed heap.

The following code example,

int i=10;

int j=i;

The values of I and J are all 10, and in memory there will be two places to store 10.

And look at the code below.

Vector x=new Vector();
x.Value=20;
Vector y=x;
Console.WriteLine(y.Value);
y.Value=50;
Console.WriteLine(x.Value);

Vector is a reference type, and a reference type requires new to instantiate one when it is used. After this code executes, there is only one vector object, and x,y points to the memory address that contains the object. Because X,y stores all references to objects, x changes as y changes. So the results of the program output are 20 and 50.

If a variable is a reference, you can set its value to NULL to indicate that it does not point to any object.

2, CTS type.

The predefined types of C # are not placed inside the language, but are placed inside the. NET framework, such as declaring an int type, actually. NET structure, an instance of System.Int32. This means that you can think of all the basic data types as classes that support certain methods.

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.