Types and variables

People

A type defines the structure and behavior of any data in C#. A type declaration can include its members, the base type, the interfaces it implements, and the operations allowed for that type. A variable is a label that refers to an instance of a particular type.

There are two kinds of types in C#: reference types and value types. Value type variables contain the data directly, and reference type variables store references to the desired data, which are named objects. Two reference type variables can refer to the same object, so it can happen that operations on one variable will affect the object to which the other variable refers. Each variable of value type has its own copy of data, and operations on one variable cannot affect the other (except for the parameter variables ref and out).

The identifier is the name of the variable. The identifier is a sequence of Unicode characters without spaces. An identifier can be a C# reserved word if it is prefixed by @. When interacting with other languages, it can be useful to use a reserved word as an identifier.

Value types in C# are divided into simple types, enum types, struct types, types that allow NULL values, and tuple value types. Reference types in C# are divided into class types, interface types, array types, and delegate types.

C# programs use type declarations to create new types. A type declaration specifies the name and members of the new type. The six categories of types in C# are user-defined: class types, structure types, interface types, enumeration types, delegate types, and tuple value types. You can also declare record types, either record struct or record class. Record types have members synthesized by the compiler. Records are mainly used to store values with minimal associated behavior.

A class type defines a data structure that contains member data (fields) and member functions (methods, properties, etc.). Classes support single inheritance and polymorphism mechanisms, which allow you to create derived classes that extend and refine the definitions of the base classes.

  • The struct type is similar to a class type in that it represents a structure with member data and member functions. But unlike classes, structures are value types and usually do not require allocating memory from the heap. Structure types do not support user-defined inheritance, and all structure types inherit implicitly from the object type.
  • The interface type defines a contract as a named set of open elements. An object of type class or struct that implements an interface must provide implementations for all interface elements. The interface type can inherit from multiple basic interfaces, and class or struct can implement multiple interfaces.
  • The delegate type represents references to methods with a specific parameter list and return value type. Delegates allow methods to be used as entities, storing them in variables and passing them as parameters. Delegates are similar to the function types used in functional languages. Their working principle is similar to function pointers from some languages. Unlike function pointers, delegates are object-oriented and type-safe.
  • The class, struct, interface, and delegate types support universal patterns that allow you to pass other types to them as parameters.

C# supports unidimensional and multidimensional arrays of any type. Unlike the types listed above, array types do not need to be declared before they can be used. Array types can be formed simply by entering square brackets after the type name. For example, int[] is a one-dimensional array of int type values, while int[,] is a two-dimensional array of int type values, whereas int[][] is a one-dimensional array of one-dimensional arrays (or arrays of arrays) of int type values.

Edit