Variables and Data Types in .NET
Understanding variables and data types is fundamental to programming in .NET. Variables are memory locations that hold values, and data types define the kind of data a variable can store and the operations that can be performed on it.
Value Types vs. Reference Types
In .NET, types are broadly categorized into two main groups: value types and reference types.
- Value Types: Store their data directly within their own memory. Examples include primitive types like
int
,float
,bool
, and structures (struct
). When you assign a value type variable to another, the value is copied. - Reference Types: Store a reference (memory address) to the actual data, which is located on the heap. Examples include classes, interfaces, delegates, and arrays. When you assign a reference type variable to another, both variables point to the same object in memory.
Primitive Data Types
C# provides a rich set of built-in data types to represent various kinds of data. Here are some of the most common primitive types:
Type | Description | Size (Bytes) | Range |
---|---|---|---|
sbyte |
Signed 8-bit integer | 1 | -128 to 127 |
byte |
Unsigned 8-bit integer | 1 | 0 to 255 |
short |
Signed 16-bit integer | 2 | -32,768 to 32,767 |
ushort |
Unsigned 16-bit integer | 2 | 0 to 65,535 |
int |
Signed 32-bit integer | 4 | -2,147,483,648 to 2,147,483,647 |
uint |
Unsigned 32-bit integer | 4 | 0 to 4,294,967,295 |
long |
Signed 64-bit integer | 8 | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
float |
Single-precision floating-point number | 4 | Approx. ±1.5e-45 to ±3.4e38 |
double |
Double-precision floating-point number | 8 | Approx. ±5.0e-324 to ±1.7e308 |
decimal |
128-bit precise decimal value | 16 | ±1.0 x 10^-28 to ±7.9 x 10^28 |
bool |
Boolean (true or false) | 1 (implementation specific) | true or false |
char |
Unicode character | 2 | U+0000 to U+FFFF |
string |
Sequence of Unicode characters | Variable | Represents text |
Declaring and Initializing Variables
Variables must be declared with a specific data type and can optionally be initialized with a value. The var
keyword can be used for implicit typing, where the compiler infers the type from the assigned value.
// Explicit declaration and initialization
int age = 30;
string name = "Alice";
double salary = 55000.75;
bool isEmployed = true;
// Implicit declaration using var
var city = "New York"; // Compiler infers string
var count = 100; // Compiler infers int
Data Type Conversion (Casting)
Sometimes, you need to convert a value from one data type to another. This is known as casting. There are two types of casting:
- Implicit Casting: Occurs automatically when converting a smaller data type to a larger one (e.g.,
int
tolong
). No data is lost. - Explicit Casting: Requires explicit syntax (type casting operator) when converting a larger data type to a smaller one (e.g.,
double
toint
). This can lead to data loss or truncation.
// Implicit casting
int myInt = 10;
long myLong = myInt; // myLong is now 10
// Explicit casting
double myDouble = 9.78;
int myInteger = (int)myDouble; // myInteger is now 9 (data loss)
// Using Convert class for safer conversions
string numberString = "123";
int numberInt = Convert.ToInt32(numberString);
Nullability
Value types are non-nullable by default, meaning they must always have a value. Reference types, on the other hand, can be assigned the special value null
, indicating that they do not refer to any object.
With nullable reference types (introduced in C# 8.0), you can explicitly indicate whether a reference type is expected to be nullable, improving code safety and reducing null reference exceptions.
// Nullable value type
int? nullableInt = null; // Can hold an integer or null
// Reference type (nullable by default pre-C# 8.0)
string nullableString = null;
// Non-nullable reference type (with nullable reference types enabled)
// string nonNullableString = null; // This would generate a warning/error
Key Takeaways
- Variables hold data in memory.
- Data types define the nature of the data.
- Value types store data directly; reference types store references.
- .NET provides numerous built-in primitive types for common data.
- Casting allows type conversions, but be mindful of potential data loss.