Data types in C# define the type of data that a valid C# variable can hold. C# is a strongly typed programming language; each data type (integer, character, float, etc.) is predefined as part of the programming language. All constants or variables are defined for a specific program with one of the data types. To explore the details of data types in C#, readers can refer to the following article by Mytour.
Data Types in C#
1. Data types in C#.
2. Value data type.
3. Reference data type.
4. Pointer data type.
1. Data types in C#
Data types in C# are divided into 3 main types:
- Value data type
- Reference data type
- Pointer data type
2. Value data type
In C#, value data types store variable values directly in memory and accept both signed and unsigned constants. The derived class for this data type is System.ValueType.
Here are the value data types in the C# programming language:
- Signed and Unsigned Integral Data Types: There are 8 Integral data types supporting signed and unsigned 8-bit, 16-bit, 32-bit, and 64-bit values.
- Floating Point Data Types: There are 2 Floating Point data types for decimal data.
+ Float: This is a single-precision 32-bit floating point data type with 7-digit precision. To initialize a float variable, use the suffix f or F.
+ Double: This is a single-precision 64-bit floating point data type with 14-15 digit precision. To initialize a double variable, use the suffix d or D. However, it's not mandatory to use the suffix as floating point types default to double data type.
- Decimal Data Type: The Decimal data type is a 128-bit type suitable for financial and currency calculations, with a precision of 28-29 digits. To initialize a decimal variable, use the suffix m or M. If the suffix m or M is not used, it is considered double by default.
- Character Data Type: The Character data type represents a UTF-16 encoded code unit or Unicode character, 16-bit in size.
Example
- Example 1:
- Examples of Data Types in C#:
Output Result:
- Example 2:
Another Example of Data Types in C#:
- Output Result:
- Boolean Data Type: This data type must be assigned the value True or False. The value of the Boolean data type cannot be converted to any other value type. However, programmers can easily write conversion code.
3. Reference Data Type
Reference data types contain the memory address of the variable's value because reference types do not store the variable's value directly in memory. The available reference data types are string and object.
- String Data Type: Represents a sequence of Unicode characters, with the type name being System.String. Note that string and String are equivalent.
- Object Data Type: In C#, there are various data types, including predefined and user-defined types, reference types, and value types, types directly or indirectly inherited from Object. The Object data type is essentially the foundation for all data types in C#. Before assigning a value, it undergoes type conversion. When a variable of a value data type is converted to an object, it is called boxing. When a variable of the object data type is converted to a value data type, it is called unboxing. The type name of the Object data type is System.Object.
Về cơ bản kiểu dữ liệu Object là nền tảng, cơ sở cho tất cả các kiểu dữ liệu trong C#. Trước khi gán giá trị, nó sẽ chuyển đổi kiểu dữ liệu. Khi một biến của kiểu dữ liệu giá trị được chuyển đổi thành object, nó được gọi là boxing. Khi một biến của kiểu dữ liệu object được chuyển đổi thành kiểu dữ liệu giá trị, nó được gọi là unboxing. Type name của kiểu dữ liệu Object là System.Object.
Pointer data type contains the memory address of a variable's value.
- Pointer Data Type: Pointer data type contains the memory address of a variable's value.
To delve into the details of the pointer data type, we use two symbols: ampersand (&) and asterisk (*). Specifically:
- Ampersand (&): also known as the Address operator, used to determine the address of a variable.
- Asterisk (*): also known as the Indirection operator, used to access the value at an address.
Syntax:
type* identifier;
In this article, Mytour has introduced you to the data types in C#. Additionally, readers can explore more articles on Mytour to gain a deeper understanding of basic C# syntax and C# program structure. In the next article, Mytour will further introduce you to type conversion in C#.