Understand C # series/Core C #/data types,

Source: Internet
Author: User

Understand C # series/Core C #/data types,
Data Type

C # divide data types into two types: Value Type and reference type.

Value Type and reference type

A Value Type Variable directly points to a stored value, and a reference type variable points to a value reference. [Understanding: referencing is like the number plate of a cabinet. You can find the cabinet in a different way. If you assign a value to a variable of the Value Type again, the value will be changed. If you assign a value to a variable of the reference type again, you just change the number card, pointing to other cabinets, the former cabinet is not modified.]

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

Differences in use
int i=20;int j=i;

Int Is a variable of the integer type. The integer type belongs to the value type. j copies the I value when assigning values. Therefore, this stores the value 20 in two places in the memory. If one of the variables is changed, it does not affect the other one.

Apple a = new Apple();a.color = "red";Apple b = a;b.color = "green";

Apple is the Apple type we define. We use new Apple () to create an actual Apple and use the referenced variable a to point to the Apple.

In the second row, the color of the apple is set to red.

In the third row, we added a new variable and assigned a value for the data of the reference type. Therefore, B also pointed to the Apple pointed to by a, and did not generate the second apple.

In the fourth row, we changed the green color of the apple that B points to. In this case, we can see that a. color also shows that the color of the apple is green, because there is always only one apple.

Apple a = null;

This means that a variable of the apple type does not point to any actual Apple. If you want to use this variable a to represent the actual Apple, an error will be reported because there is nothing available, how can I use it. Null values cannot be assigned to variables of the value type, which is a patent of the reference type.

When no variable points to an instance, the instance cannot be accessed. the CLR Garbage Collector regularly searches for such instances and deletes them, return the memory they occupy to the operating system.

Predefined Data Types

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

There are 8 Integer types in the 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 }.

Among the 13 value types, there are two floating point types: {float: 32-Bit Single-precision floating point number} {double: 64-bit double-precision floating point number }.

Among the 13 value types, there are: {decimal: the 128-bit high-precision decimal notation that is usually used for financial computing} {bool: Used to include boolean values true or false} {char: to save the value of a single character }.

Value Type Assignment Method:

Int I = 1234; uint ui = 1234U; long l = 1234L; long x = 0x12ab; // Add the 0x prefix ulong ul = 1234UL to assign values in hexadecimal notation; float f = 12.3 Fdouble d = 12.3; decimal d = 12.30 M; bool B = true; char c = 'a'; char c = '\ n'; // Escape Character

2 pre-defined reference types: {object: root type, object-oriented concept} {string: string type, but has many Value Type features, so that it is often mistaken for Value Type}

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.