What is a type?
When a variable is declared in C#, it is allocated an area in memory.
The type of a variable depends on the type of the value to be assigned.
Programmers should be aware of the difference between the types of variables since not putting a value in the proper type will result in an error.
There are three types: built-in types, user-defined types, and types synthesized from other types.
There are value types and reference types, each of which is classified into a specific type.
See also:
The entire list is as follows:
Built-in types
Value types | Reference types |
sbyte, byte, short, ushort, int, uint long, ulong, float, double, char, bool, decimal | string, object |
User defined types
Value types | Reference types |
struct, enum | class, interface, delegate |
Types synthesized from other types
Value type | Reference type |
Nullable | Array |
A value type is a type that stores a value directly in a variable, and a reference type holds reference information (address) such as where the value is actually located.
List of types
Built-in types
Built-in types" are types originally defined in the C# specification.
The following is a list of built-in types:
Value types:
Type name | Range |
sbyte | -128 to 127 |
byte | 0 to 255 |
short | -32,768 to 32,767 |
ushort | 0 to 65,535 |
int | -2,147,483,648 to 2,147,483,647 |
unit | 0 to 4,294,967,295 |
long | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
ulong | 0 to 18,446,744,073,709,551,615 |
float | ±1.5 x 10−45 to ±3.4 x 1038 |
double | ±5.0 × 10−324 to ±1.7 × 10308 |
bool | true or false |
char | U+0000 to U+FFFF |
decimal | ±1.0 x 10-28 to ±7.9228 x 1028 |
A bit is a single binary digit, where each is represented by only 0 or 1.
In other words, an 8-bit integer is an integer represented by eight binary digits.
Reference types
Type name | Description |
string | character string |
object | Base type for all types |
These are the built-in types. The most commonly used types vary from field to field, but int, boolean, string, and double are the types often seen in teaching materials.
User-defined types
User-defined types represent classes whose internals must be defined by the programmer.
User-defined types include the following types:
Value types:
Type name | Description |
struct | Can have members (data and methods). Cannot inherit. |
enum | Enumerated type. Used to store data that can only take fixed values, such as days of the week. |
Reference types
Type name | Description |
class | Can have members (data and methods). Can also inherit |
interface | It can have only abstract members. |
delegate | Type to refer to method |
Types synthesized from other types
Nullable
Nullable is also a value type.
Null refers to an invalid value itself.
The declaration must be followed by a "? (question) in the declaration. (variable name = value) with a question mark "?
Arrays
Arrays are a type of reference type used to combine multiple values and assign them to a single variable.
The declaration is made using parentheses ( []), as in "type name[] variable name = new type name [length of array]".