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


Search other websites:

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?

Differences between value types (value types and reference types) can be determined by the following characteristics.

1. A method for storing an object (variable) in the computer memory.

Objects (variables) of value types are placed on the stack completely. Objects of reference types are placed according to the following principle:

  • a reference to the object data is placed on the stack;
  • the “managed heap” directly stores the data (fields) of the object. This data is pointed to by a reference from the stack.

2. The difference is in the base type.

For value types, the base type is System.ValueType. For reference types, the base type can be any unsealed (non-sealed) type other than System.ValueType.

3. The ability to be inherited.

Valuable types are always sealed, and therefore cannot be inherited. Reference types can be basic (inherited) for other types, if they are not sealed (not denoted as sealed).

4. A way of passing parameters to a function.

Variables of value types are passed by value. This means that a complete copy of the argument variable is passed to the function. In the case of reference types, when passing a reference to a function, only the reference itself is copied by value.

5. Overriding the System.Object.Finalize() method.

Value types do not need to be finalized because they are never allocated on a managed heap, unlike reference types.

6. The lifetime of a variable.

Value types cease to exist when they go beyond the context in which they were defined. Reference types are destroyed by the garbage collector.

What value types and reference types have in common is that you can define constructors for both.

 

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.

 

11. Assignment operation =. Applying assignment operation for value types and reference types

For value types and reference types, the assignment operation has its own characteristics. Let’s consider them on an example of assignment of two variables.

1. Assignment of value types.

Value types include variables of basic types (int, double, char, and others), structural variables (struct), and enumerations (enum). In general, such variables are derived from the System.ValueType class. When assigning such variables of the form

var1 = var2;

the value of var2 is copied to var1. Thus, var1 contains a copy of var2. Further changes in the value of one variable will not affect the value of another variable.

2. Assignment of reference types.

Reference types are types that are described by classes. When assignment variables of a reference type, which are class instances or objects,

MyClass ref1;
MyClass ref2;
...
ref1 = ref2; // assignment of references
...

the value of ref2 is copied to ref1. As a result, both references contain the address of the object pointed to by ref2. The object itself is not copied. Thus, both references point to the same object. When developing programs, this feature must be taken into account.

Example. The example demonstrates the use of assignment for value-type variables and variables of a reference type. An example is implemented for an application such as Console Application.

using System;

namespace ConsoleApp2
{
  // Class Integer - reference type
  class Integer
  {
    public int value; // integer value
  }

  class Program
  {
    static void Main(string[] args)
    {
      // Assignment operation for value types
      int a, b;
      a = 8;

      // the value of the variable a is copied to the variable b
      b = a; // b = 8, a = 8
      Console.WriteLine("a = {0}, b = {1}", a, b);

      a = 6; // a = 6, b = 8 - variables are located in different memory locations
      Console.WriteLine("a = {0}, b = {1}", a, b);

      // Assignment operation for reference types
      // Declare two reference variables of type Integer
      Integer d1, d2;

      // allocate memory for reference d1
      d1 = new Integer(); 
      d1.value = 37; // fill with values

      // allocate memory for reference d2
      d2 = new Integer();
      d2.value = 50; // fill with values

      // reference assignment operation - only references are copied
      d2 = d1; // d2.value = 37
      Console.WriteLine("d1.value = {0}, d2.value = {1}", d1.value, d2.value);

      d2.value = 100; // d1.value = 100 - references point to the same object (memory location)
      Console.WriteLine("d1.value = {0}, d2.value = {1}", d1.value, d2.value);
    }
  }
}

The result of the program

a = 8, b = 8
a = 6, b = 8
d1.value = 37, d2.value = 37
d1.value = 100, d2.value = 100

As you can see from the result, when assigning references

d2 = d1;

both references point to memory allocated for d1. Change value in d2 reference

d2.value = 100;

changes the value in the reference d1: d1.value = 100.

 


Related topics