C# Data Types

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 typesReference types
sbyte, byte, short, ushort, int, uint
long, ulong, float, double, char,
bool, decimal
string, object

User defined types

Value typesReference types
struct, enumclass, interface, delegate

Types synthesized from other types

Value typeReference type
NullableArray

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 nameRange
sbyte-128 to 127
byte0 to 255
short-32,768 to 32,767
ushort0 to 65,535
int-2,147,483,648 to 2,147,483,647
unit0 to 4,294,967,295
long-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
ulong0 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
booltrue or false
charU+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 nameDescription
stringcharacter string
objectBase 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 nameDescription
structCan have members (data and methods). Cannot inherit.
enumEnumerated type. Used to store data that can only take fixed values, such as days of the week.

Reference types

Type nameDescription
classCan have members (data and methods). Can also inherit
interfaceIt can have only abstract members.
delegateType 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]".


See also:
C# List Contains with Examples
C# typeof with Examples
C# TryParse (int, double, float) with Examples
C# Variables
C# IndexOf() – List, Array, ArrayList, String