Understanding data types is fundamental to programming in C#. Data types define the kind of values a variable can hold and the operations that can be performed on it. C# is a strongly-typed language, meaning every variable and expression has a type that is known at compile time.
C# distinguishes between two main categories of types:
C# provides a rich set of numeric types to handle different ranges and precision of numbers.
Type | Size | Range | Description |
---|---|---|---|
sbyte |
8 bits | -128 to 127 | Signed 8-bit integer |
byte |
8 bits | 0 to 255 | Unsigned 8-bit integer |
short |
16 bits | -32,768 to 32,767 | Signed 16-bit integer |
ushort |
16 bits | 0 to 65,535 | Unsigned 16-bit integer |
int |
32 bits | -2,147,483,648 to 2,147,483,647 | Signed 32-bit integer (most common) |
uint |
32 bits | 0 to 4,294,967,295 | Unsigned 32-bit integer |
long |
64 bits | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | Signed 64-bit integer |
ulong |
64 bits | 0 to 18,446,744,073,709,551,615 | Unsigned 64-bit integer |
float |
32 bits | +/- 1.5 x 10-45 to +/- 3.4 x 1038 (approx.) | Single-precision floating-point number |
double |
64 bits | +/- 5.0 x 10-324 to +/- 1.7 x 10308 (approx.) | Double-precision floating-point number (default for literals) |
decimal |
128 bits | +/- 1.0 to 28-29 significant digits | High precision for financial and monetary calculations |
These types represent single characters and truth values.
Type | Description |
---|---|
char |
Represents a single Unicode character (16 bits). Enclosed in single quotes, e.g., 'A' . |
bool |
Represents a boolean value: true or false . |
The string
type represents a sequence of characters.
Type | Description |
---|---|
string |
Represents a mutable sequence of characters. Enclosed in double quotes, e.g., "Hello, World!" . It's a reference type. |
double
is often sufficient for general floating-point calculations, use decimal
when exact precision is critical, especially in financial applications to avoid rounding errors.
To declare a variable, you specify its type followed by its name. You can optionally assign an initial value using the assignment operator =
.
// Declaring variables
int age;
string name;
bool isComplete;
double price;
// Declaring and initializing variables
int count = 100;
string message = "Welcome to C#!";
bool isActive = true;
decimal salary = 50000.50m; // 'm' suffix indicates decimal literal
var
Keyword)C# 3.0 introduced the var
keyword, which allows the compiler to infer the type of a variable from the initializer. The variable is still strongly typed, but the type declaration is implicit.
var quantity = 50; // Compiler infers 'int'
var userName = "Alice"; // Compiler infers 'string'
var pi = 3.14159; // Compiler infers 'double'
var isStudent = false; // Compiler infers 'bool'
// You cannot declare a variable with 'var' without initializing it:
// var someValue; // Error!
// You cannot change the inferred type later:
// quantity = "one hundred"; // Error!
Sometimes, you need to convert a value from one type to another. This process is called type casting. There are two types of casting:
int
to long
).double
to int
).
// Implicit casting
int myInt = 10;
long myLong = myInt; // myLong is now 10
// Explicit casting
double myDouble = 9.78;
int myIntFromDouble = (int)myDouble; // myIntFromDouble is now 9 (decimal part is truncated)
Console.WriteLine(myIntFromDouble); // Output: 9
System
NamespaceMany common types, like string
, int
, bool
, etc., are implicitly available. However, for more advanced operations or to access specific functionalities, you might need to use types from the System
namespace or its sub-namespaces. For instance, the Convert
class provides methods for explicit type conversions.
string numberString = "123";
int convertedInt = Convert.ToInt32(numberString); // Converts string to int
double piValue = 3.14159;
string piAsString = Convert.ToString(piValue); // Converts double to string
// Using built-in methods for type checking and conversion is often preferred.
Mastering these fundamental data types will equip you to handle a wide range of programming tasks in C#.