C# data types, with value and reference types

Lionsure 2020-09-04 Original by the website

Data types are divided into value types and reference types in C#. Value types are divided into "structures and enumerations". The structures include "Numeric type, bool(Boolean) and user-defined structure", Numeric type includes "integer, floating-point and decimal". Reference types are divided into "class (Object, String), delegate, Array and interface".

 

C# data types

I. Value types

I) Numerical

1. Integer

1) sbyte

The sbyte keyword represents an integer, ranging from -128 to 127, and is a signed 8-bit integer.

 

2) byte

The byte keyword represents an integer type, ranging from 0 to 255, an unsigned 8-bit integer.

 

3) char

The char is used to declare Unicode characters ranging from U+0000 to U+ffff. 16-bit Unicode characters are used to represent most known written languages in the world.

 

4) short

The short represents an integer data type, ranging from -32,768 to 32,767, a signed 16-bit integer.

 

5) ushort

The ushort represents an integer data type, ranging from 0 to 65,535, an unsigned 16-bit integer.

 

6) int

The int keyword represents an integer type, -2,147,483,648 to 2,147,483,647, a signed 32-bit integer.

 

7) uint

The uint keyword represents an integer type, from 0 to 4,294,967,295, an unsigned 32-bit integer.

 

8) long

The long represents an integer type, -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807, a signed 64-bit integer.

 

9) ulong

The ulong represents an integer type, from 0 to 18,446,744,073,709,551,615, an unsigned 64-bit integer.

 

 

2. Floating point

1) float

The float represents a simple type that stores 32-bit floating point values, with a range of ±1.5e-45 to ±3.4e38, and an accuracy of 7 bits.

 

2) double

The double represents a simple type that stores 64-bit floating-point values, ranging from ±5.0e-324 to ±1.7e308, 15 to 16 bits.

 

3. decimal

The decimal represents a 128-bit data type, ranging from ±1.0 × 10e-28 to ±7.9 × 10e28; precision is 28 to 29 effective digits. Compared with floating-point type, decimal type has higher precision and smaller range, which makes it suitable for financial and monetary calculations.

 

II) Boolean type(bool)

The bool is an alias of System.Boolean, used to declare variables to store the Boolean values ??true and false.

 

 

II. Reference types

I) Built-in reference types

1. string

The string type represents a sequence of zero or more Unicode characters. It is an alias for String in the .NET Framework.

 

2. object

The object type is an alias of Object in .NET Framework. In the unified type system of C#, all types(predefined types, user-defined types, reference types and value types) are directly or indirectly inherited from Object; values ??of any type can be assigned to variables of object. The process of converting a variable of a value type into an object is called "boxing"; the process of converting a variable of an object type into a value type is called "unboxing".

 

II) Reference types variable(object)

1. class

The class is used to declare a class. A class can include "constructors, destructors, constants, fields, properties, methods, indexers, operators, events, delegates, classes, interfaces, structures".

 

2. interface

The interface only contains the signatures of methods, delegates or events, and the realization of methods is done in the class that implements the interface.

 

3. delegate

The delegate is used to declare a reference type, which can be used to encapsulate named methods or anonymous methods. Delegates are similar to function pointers in C++; however, delegates are type safe and reliable.