C# data types

Value types

  • Value type variables can be assigned directly to a value. They are derived from the class System.valueType.
  • Value types directly contain data. Such as int, char, and float, which store numbers, characters, and floating-point numbers respectively. When you declare an int, the system allocates memory to store the value.

Value types can be roughly divided into the following categories:

type For example,
Integer types Sbyte, BYTE, Short, USHORT, INT, UINT, Long, ULONG, and CHAR
floating-point Float and double
Decimal type decimal
Boolean type True or false, the specified value
Empty type Nullable data type

Reference types

  • Reference types do not contain the actual data stored in variables, but they do contain references to variables.
  • In other words, they refer to a memory location. When multiple variables are used, the reference type can point to a memory location. If the memory location data is changed by one variable, the other variables automatically reflect the change in value. The built-in reference types are: Object, Dynamic, and String.

– Object Indicates the Object type

The Object Type is the ultimate base class for all data types in C#’s Common Type system-cts. Object is an alias of the System.Object class. So an Object type can be assigned a value of any other type (value type, reference type, predefined type, or user-defined type). However, before assigning a value, a type conversion is required.

When a value type is converted to an object type, it is called boxing *; On the other hand, when an object type is converted to a value type, it is called unboxing.

object obj;
obj = 100; // This is the case
Copy the code

– Dynamic indicates the Dynamic type

You can store any type of value in a dynamic data type variable. Type checking for these variables occurs at run time.

dynamic <variable_name> = value;
Copy the code

Such as:

dynamic d = 20;
Copy the code

Dynamic types are similar to object types, but type checking for object type variables occurs at compile time, whereas type checking for dynamic type variables occurs at run time.

– The value is a String

The String type allows you to assign any String value to a variable. The String type is an alias of the System.String class. It is derived from the Object type. String values can be assigned in two forms: quotes and @ quotes. Such as:

String str = "runoob.com";
Copy the code

An @ quoted string:

@"runoob.com";
Copy the code

A string can be preceded by an @ (called a “verbatim string”) to treat an escaped character (\) as a normal character, such as:

string str = @"C:\Windows";
Copy the code

Is equivalent to:

string str = "C:\\Windows";
Copy the code

Any newline in the @ string is allowed, and newlines and indent Spaces count to the length of the string.

string str = @"<script type=""text/javascript""> <! -- --> </script>";
Copy the code

User-defined reference types can be class, Interface, or Delegate.

Pointer types

A pointer type variable stores the memory address of another type. Pointers in C# have the same functionality as Pointers in C or C++. Syntax for declaring pointer types:

type* identifier;
Copy the code

Such as:

char* cptr;
int* iptr;
Copy the code