Understanding C # Series/core C #/data types

Source: Internet
Author: User

Data type data type

C # divides data types into two kinds: value types and reference types.

Value types and reference types

A variable of a value type directly points to the stored value, and the reference type's variable points to a reference to the value. [Understanding: The reference is like a cupboard number card, you can follow the clues to find the cabinet.] If you assign a value to a variable of a value type again, then it will change the value, if you once again assign a value to a reference type variable, then just change a number card, point to the other cabinets, the previous cabinet is not modified. ]

These two types of data are stored in different places in memory, value type data is stored on the stack, and reference type data (a reference to a value) is stored on the managed heap.

Differences in the use of
int i=; int j=i;

int defines a variable of integer type, the integer type is a value type, J copies the value of I at the time of assignment, so this stores the value 20 in two places of memory, and if one is changed, it does not affect the other.

New"red"="green";

Apple is our definition of apple, and we created an actual apple with new Apple and pointed to the created Apple with a reference-type variable A.

The second line sets the apple color to be red.

In the third line we add a new variable that assigns the data of a reference type, so B also points to the Apple that points to a, and does not produce a second apple.

In line fourth, we change the color of the apple that the B points to green, when actually looking at the A.color will also find that the apple color is green, because there is always only one apple.

null;

This means that an Apple-type variable does not point to any actual apple, and if you want to use the actual apple that the variable a represents, you will get an error because there is nothing to use. A variable of a value type cannot be assigned a value of NULL, which is a patent of a reference type.

When there is no one variable pointing to an instance, the instance cannot be accessed, and the CLR's garbage collector periodically searches for such instances, removes them, and returns the memory they occupy to the operating system.

Pre-defined data types

C # has 15 predefined types, 13 of which are value types, and two are reference types (string and object)

There are 8 integers in 13 value types: {Positive and negative: 8-bit sbyte,16 bit short,32 bit int,64 bit long}{unsigned: 8-bit byte,16 bit ushort,32 bit uint,64 bit ulong}.

There are 2 floating-point types in 13 value types: {float:32-bit single-precision floating-point}{double:64-bit double-precision floating-point number}.

13 value types are also: {decimal: 128-bit high-precision decimal notation typically used for financial calculations}{bool: Used to contain Boolean values of TRUE or False}{char: In order to hold the value of a single character}.

Value type Assignment method:

inti =1234;UINTUI =1234U;LongL =1234L;Longx =0x12ab;//add a 0x prefix to the 16 binary assignmentULONGUL =1234UL;floatf =12.3FDoubleD =12.3;decimalD =12.30M;BOOLb =true;Charc ='A';Charc ='\ n';//Escape Character

2 Predefined reference types: {object: Root type, object-oriented concept}{string: String type, but has many attributes of value type, making it often mistaken for value type}

Understanding C # Series/core C #/data types

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.