On C # Learning experience--data type

Source: Internet
Author: User

One of the technical books I've read is written in this way:

"Object-oriented programming absorbs the essence of structured programming and combines it with some new concepts, resulting in a different and better way to organize programs." In most cases, the program has two organizational methods: the code (the object that performs the action) is centered or the data (the affected object) is centered. Programs are typically organized as code-centric when programming using only a structured approach. This approach can be thought of as "code for Data".

Object-oriented programs use a different approach, which organizes programs in a data-centric way, and its key principles are "data-control access to code." In object-oriented languages, data and the corresponding code that acts on that data are defined, so data types are used to precisely define the operations that apply to that data. "   

--excerpt from the c#4.0 full reference Manual (US) Herbert Schildt

C # is a strongly typed language, and all operations are checked by the compiler type, and illegal operations are not compiled, which helps prevent errors and enhance the reliability of the program. In C #, all variables, expressions, and values have their type.

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

The difference between the two types is what the variable holds. For a value type, the variable holds the actual value, and for the reference type, it is a reference to the value that is saved.

In C #, all data types are defined in a platform-independent manner for future C # and. NET migrations to other platforms

1. Value type: C # consists of 13 types of values, often referred to as simple types or basic types.

1) Integer: There are 8 predefined types of shaping. As the following table:

name CTS Type Description Range
SByte System.SByte 8-bit signed integer -128~127 ( -2^7~2^7-1)
Short System.Int16 16-bit signed short integer -32768~32767 ( -2^15~2^15-1)
Int System.Int32 32-bit signed integer -2147483648~2147483647 ( -2^31~2^31-1)
Long System.Int64 64-bit signed long integer -9223372036854775808~9223372036854775807 ( -2^64~2^64-1)
Byte System.Byte 8-bit unsigned shaping 0~255 (0~2^8-1)
UShort System.UInt16 16-bit unsigned short integer 0~65535 (0~2^16)
UInt System.UInt32 32-bit unsigned integer 0~4294967295 (0~2^32-1)
ULong System.UInt64 64-bit unsigned long integer 0~18446744073709551615 (0~2^64-1)

C # defines the signed and unsigned forms of various integral types. The difference between a signed integer and an unsigned integer is in how the integer higher order bit is handled. For signed integers, the compiler generates code that takes the higher-order bits of an integer as a sign bit, 0 for integrity, and 1 for negative values.

2) floating-point type: Divided into single-and double-precision values. As the following table:

name CTS Type Description Number of digits Range
Float System.Single 32-bit single-precision floating-point number 7 1.5e-45~3.4e+38
Double System.Double 64-bit double-precision floating-point number 15/16 5e-324~1.7e+308

If a non-integer value is hard-coded in code, the compiler assumes that the variable is double, and if you want to specify that the value is float, you can add the character F (or F) after it. For example: float f=12.5f

3) Decimal type: A floating-point number that represents a higher precision, as in the following table:

name CTS Type Description Number of digits Range
Decimal System.Decimal 128-bit high-precision decimal number representation 28 1e-28~7.9e+28

This type is mostly used for financial calculations, not basic types, so there is a performance penalty for using that type in the calculation.

If you want to make the number a decimal type, you can add the character m (or m) after the number. Example: Decimal d=12.60m

4) BOOL Type: Used to contain a Boolean value of TRUE or false, as in the following table:

name CTS Type Description Number of digits value
bool System.Boolean Represents true or False NA True or False

NOTE: BOOL values and integers cannot be implicitly converted, and if a variable (or a function's return type) is declared as a bool type, only the value TRUE or false can be used. If you attempt to use 0 to indicate false, a value other than 0 indicates true, an error occurs. 5) Character type: In order to save a single character value, as in the following table:

name CTS Type value
Char System.Char Represents a 16-bit (Unicode) character

Note: The literal of the char type is enclosed in single quotation marks, and if the character is placed in double quotation marks, the compiler will treat it as a string, resulting in an error.

In addition to char representing character literals, you can also use 4-bit hexadecimal Unicode values, integer values with data type conversions, or hexadecimal numbers to represent them. They can also be represented by escape sequences.

6) C # also defines other three kinds of value types: Enumeration (enum), struct (struct), and nullable type.

2. Reference type: C # supports two predefined reference types, such as the following table:

name CTS Type Description
Object System.Object Root type, other types in the CTS are derived from it (including value types)
String System.String Unicode string

1) Object type: In C #, the object type is the final parent type, and all built-in types and user-defined types derive from it.

2) String Type:

Although the assignment of a string type is like an assignment of a value type, string is a reference type.

There are some differences between string and reference type, such as: the string is immutable. Modifying one of the strings creates a completely new string object, and the other string has no change.

String literals are placed in double quotation marks.

On C # Learning experience--data type

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.