C# Data Types
Data types define the kind of data a variable can hold and the operations that can be performed on it. C# is a strongly-typed language, meaning that every variable and expression has a type that is known at compile time. This helps in catching errors early and improving code reliability and performance.
Value Types vs. Reference Types
In C#, data types are broadly categorized into two main groups:
- Value Types: Variables of value types directly contain their data. When you assign a value type variable to another, the value is copied. Examples include primitive types like
int,float,bool, and structures (struct). - Reference Types: Variables of reference types store a reference (or pointer) to the actual data, which is stored on the heap. When you assign a reference type variable to another, only the reference is copied, meaning both variables point to the same object. Examples include classes, arrays, delegates, and interfaces.
Built-in Data Types
C# provides a rich set of built-in data types, categorized as follows:
Numeric Types
These types represent numerical values.
| Type | Description | Range | Size |
|---|---|---|---|
sbyte |
Signed 8-bit integer | -128 to 127 | 8 bits |
byte |
Unsigned 8-bit integer | 0 to 255 | 8 bits |
short |
Signed 16-bit integer | -32,768 to 32,767 | 16 bits |
ushort |
Unsigned 16-bit integer | 0 to 65,535 | 16 bits |
int |
Signed 32-bit integer | -2,147,483,648 to 2,147,483,647 | 32 bits |
uint |
Unsigned 32-bit integer | 0 to 4,294,967,295 | 32 bits |
long |
Signed 64-bit integer | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | 64 bits |
ulong |
Unsigned 64-bit integer | 0 to 18,446,744,073,709,551,615 | 64 bits |
float |
Single-precision floating-point number | Approx. ±1.5e-45 to ±3.4e38 | 32 bits |
double |
Double-precision floating-point number | Approx. ±5.0e-324 to ±1.7e308 | 64 bits |
decimal |
128-bit precise decimal value | Approx. ±1.0m to ±7.9e28 (28-29 significant digits) | 128 bits |
Boolean Type
| Type | Description |
|---|---|
bool |
Represents logical values: true or false. |
Character Type
| Type | Description |
|---|---|
char |
Represents a single Unicode character. Enclosed in single quotes (e.g., 'A'). |
String Type
string is a reference type representing a sequence of Unicode characters. It is a .NET alias for System.String.
Example:
string greeting = "Hello, World!";
string name = "Alice";
string combined = greeting + " " + name;
Common Type System (CTS)
C# integrates with the .NET Common Type System (CTS). This system defines a set of types that can be used across different .NET languages. Most of the built-in C# types have a corresponding CTS type. For example, int in C# corresponds to System.Int32 in the CTS.
Nullable Types
Value types in C# cannot inherently hold a null value. To represent a nullable value type, you can use the nullable type syntax:
T?(e.g.,int?,bool?)Nullable<T>(e.g.,Nullable<int>)
Nullable types are useful when a value might be absent, for instance, when reading from a database where a field could be NULL.
Example:
int? nullableInt = null;
if (nullableInt.HasValue)
{
Console.WriteLine($"Value: {nullableInt.Value}");
}
else
{
Console.WriteLine("The value is null.");
}
User-Defined Types
Beyond the built-in types, you can define your own types using:
- Classes: Blueprints for objects, encapsulating data and behavior.
- Structs: Lightweight value types, often used for small data structures.
- Enums: Define a set of named constants.
- Interfaces: Define contracts that classes must implement.
Understanding data types is fundamental to writing effective and robust C# code. Choosing the appropriate data type can significantly impact performance and memory usage.
For more detailed information on specific data types and their usage, refer to the official Microsoft documentation.