C#. Categories (groups) of data types in C#. Value types. Reference types. Basic data types. Overview

Categories (groups) of data types in C#. Value types. Reference types. Basic data types. Overview


Contents


1. What is a data type in C#?

In C# (as in any other language), a type is a characteristic of a variable (object), constant or literal, which defines:

  • a set of allowable values;
  • internal form of data storage in a computer;
  • a set of operations allowed for this type.

 

2. What categories (groups) of data types are defined in C#?

Any data type in the C # programming language is represented by a class or a structure. Accordingly, all C# data types are divided into two categories (groups):

  • value-types, which are also called structural types or base types. Value-types are implemented by structures;
  • reference-types, which are implemented by classes.

 

3. What are the differences between value-types and reference-types?

The following differences can be distinguished between value types and reference types:

  • value-types are implemented by structures. Reference-types are implemented by classes;
  • when a variable of a significant type is declared, an instance of this variable is completely placed on the stack. When declaring a reference variable, only the reference to the instance is placed on the stack, and the instance itself (data + methods) is placed in the so-called managed heap;
  • when copying objects of a significant type, a complete duplicate of the object (instance) is created. When copying objects of a reference type, only the reference to the object is copied, so both references point to the same memory area.

 

4. Expediency of applying of each category of data types

For each category of types, the conditions for their use are determined. If a small amount of memory and speed are important, then it is more advisable to use significant types. If a large amount of memory is needed to save data, it is more advisable to use reference types. This is because the size of the managed heap is larger than the size of the stack.

 

5. The concept of a base type in .NET.

Base types are value types that are implemented by structures. In C#, any basic type corresponds to a declaration of some structure that is part of the .NET platform.

For example. The float type corresponds to the .NET structure of System.Single; type long corresponds to the .NET structure of System.Int64.

More details about using basic types in C# are described here.



 

6. The structure of the base type system in .NET

All C# built-in types are derived from the Object class. Figure 1 shows part of the structure of the basic C# language type system. As you can see from the figure, value types are inherited from the ValueType class, which is inherited from the Object class.

C#. General abbreviated hierarchical structure of the basic .NET types

Figure 1. General abbreviated hierarchical structure of the basic types of C#

 

7. Integer base data types and their .NET equivalents

A table of basic integer data types and structures corresponding to these types is shown in Figure 2. Displayed:

  • type name;
  • the name of .NET structure, which corresponds to this type;
  • the presence (absence) of a number sign;
  • the range of valid values that a variable of this type can take.

 

C#. Integer base data types and their corresponding .NET structures

Figure 2. Integer base data types and their corresponding .NET structures

 

8. Data types for floating point numbers and their .NET equivalents

Figure 3 shows the basic data types for floating point numbers and their characteristics.

 

C#. Data types for floating point numbers and their corresponding .NET structures

Figure 3. Data types for floating point numbers and their corresponding .NET structures

 

9. Character data type char. Structure System.Char

C# uses the base type char to represent character information. In .NET, this type corresponds to the System.Char structure. Each variable of type char is a symbol in the Unicode format. In this format, each character is identified by a 21-bit scalar value called a “code point”. Such a representation corresponds to the UTF-16 code form in which the code point is decoded into one or more 16-bit values.

The System.Char structure implementing the char type contains:

  • 16-bit value in the range from 0x0000 to 0xFFFF;
  • a set of methods for comparing objects of type char.

 

10. Logical data type bool. Structure System.Boolean

In the .NET base type system, the logical data type bool is the System.Boolean structure. A variable of type bool can take one of two values: true or false.

 


Related topics