Data types in C#
In C#, the language provides several built-in data types that you can use to define variables. Here are the primary data types in C#:
-
Numeric Data Types:
-
Integral types:
-
sbyte: Represents signed 8-bit integers (-128 to 127).
-
byte: Represents unsigned 8-bit integers (0 to 255).
-
short: Represents signed 16-bit integers (-32,768 to 32,767).
-
ushort: Represents unsigned 16-bit integers (0 to 65,535).
-
int: Represents signed 32-bit integers (-2,147,483,648 to 2,147,483,647).
-
uint: Represents unsigned 32-bit integers (0 to 4,294,967,295).
-
long: Represents signed 64-bit integers (-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807).
-
ulong: Represents unsigned 64-bit integers (0 to 18,446,744,073,709,551,615).
-
Floating-point types:
-
float: Represents single-precision 32-bit floating-point numbers.
-
double: Represents double-precision 64-bit floating-point numbers.
-
decimal: Represents decimal numbers with higher precision and a smaller range.
-
Character Data Type:
-
char: Represents a single Unicode character.
-
Boolean Data Type:
-
bool: Represents a Boolean value, either true or false.
-
String Data Type:
-
string: Represents a sequence of characters.
-
DateTime Data Type:
-
DateTime: Represents a date and time value.
-
Enumerations:
-
enum: Represents a set of named values.
-
Arrays:
-
Arrays can be created using any of the above data types, allowing you to store multiple values of the same type.
-
Others:
-
object: Represents a base type for all other types. Can hold values of any type.
dynamic: Represents a type whose behavior is determined at runtime.
These are the fundamental data types in C#. Additionally, C# also supports user-defined data types such as classes, structures, and interfaces, which allow you to define custom types with their own member variables and methods.