C# is a type-safe language, which means that every variable and object must have a specific data type. Here are the main data types available in C#:
Value types directly contain their data. They are stored on the stack.
Primitive Data Types:
int
: Integer type (e.g., int age = 30;
)
float
: Single-precision floating point type (e.g., float pi = 3.14f;
)
double
: Double-precision floating point type (e.g., double g = 9.81;
)
bool
: Boolean type (e.g., bool isTrue = true;
)
char
: Character type (e.g., char letter = 'A';
)
Integral Types:
byte
: 8-bit unsigned integer (e.g., byte b = 255;
)
sbyte
: 8-bit signed integer (e.g., sbyte sb = -128;
)
short
: 16-bit signed integer (e.g., short s = -32768;
)
ushort
: 16-bit unsigned integer (e.g., ushort us = 65535;
)
int
: 32-bit signed integer (e.g., int i = 2147483647;
)
uint
: 32-bit unsigned integer (e.g., uint ui = 4294967295;
)
long
: 64-bit signed integer (e.g., long l = 9223372036854775807;
)
ulong
: 64-bit unsigned integer (e.g., ulong ul = 18446744073709551615;
)
Floating Point Types:
float
: Single-precision 32-bit floating point (e.g., float f = -1.5e38f;
)
double
: Double-precision 64-bit floating point (e.g., double d = 1.7e308;
)
Decimal Type:
decimal
: 128-bit precise decimal type (e.g., decimal dec = 3.14m;
)
Boolean Type:
bool
: Boolean type (e.g., bool isHappy = true;
)
Character Type:
char
: Character type (e.g., char ch = 'A';
)
Structs and Enum Types:
User-defined value types.
Reference types store references to their data (objects), which are stored on the heap.
String Type:
string
: Sequence of characters (e.g., string name = "Hello";
)
Object Type:
object
: The base type for all other types (e.g., object obj = "Hello";
)
Array Types:
Arrays of any data type (e.g., int[] arr = new int[5];
)
Class Types:
User-defined reference types (e.g., defining a class Student
).
Interface Types:
User-defined contracts for classes (e.g., defining an interface IShape
).
Delegate Types:
References to methods (e.g., delegate void MyDelegate(int x);
)
Null Type:
null
: Represents a null reference.
These are the core data types you'll work with in C#. Each serves different purposes and offers various levels of precision or specificity depending on your needs. If you have any more inquiries or need deeper context on any of these types, just let me know!