C# is a strongly typed language, meaning each variable must be explicitly declared with a type. Variables store data in memory, and the type determines what kind of data the variable can hold.
1. Declaring Variables
To declare a variable, specify the data type, followed by the variable name:
int number; // Declaration
number = 10; // Initialization
You can also declare and initialize a variable in one step:
int number = 10;
2. Variable Types in C#
Primitive Data Types
| Data Type | Description | Example Values |
|---|---|---|
| int | Integer (whole numbers) | -2147483648 to 2147483647 |
| float | Floating-point (decimal numbers) | 3.14F, -5.67F |
| double | Double-precision floating-point | 3.14159, -0.01 |
| decimal | High precision for monetary calculations | 3.14M, -12345.67M |
| char | Single character | 'A', '9' |
| string | Sequence of characters | "Hello, World!" |
| bool | Boolean (true or false) | true, false |
| byte | 8-bit unsigned integer | 0 to 255 |
| sbyte | 8-bit signed integer | -128 to 127 |
| short | 16-bit signed integer | -32,768 to 32,767 |
| ushort | 16-bit unsigned integer | 0 to 65,535 |
| long | 64-bit signed integer | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
| ulong | 64-bit unsigned integer | 0 to 18,446,744,073,709,551,615 |
Example of Primitive Data Types
int age = 30;
float price = 19.99F;
double pi = 3.14159;
char initial = 'A';
string name = "Alice";
bool isComplete = true;
3. Nullable Variables
Variables can be nullable (accept a null value) using ?:
int? nullableInt = null;
4. Type Inference with var
The var keyword allows the compiler to infer the variable’s type based on the assigned value:
var age = 25; // Compiler infers as int
var name = "John"; // Compiler infers as string
Note: The type of a
varvariable cannot change once assigned.
5. Constants
Constants are variables whose values cannot change:
const double Pi = 3.14159;
6. Enumerations (Enums)
Enums represent a set of named constants:
enum Days { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
Days today = Days.Monday;
7. Reference Types vs Value Types
- Value Types: Store data directly (e.g.,
int,float,bool). - Reference Types: Store references to the data (e.g.,
string, objects).
8. Type Casting
C# supports both implicit and explicit type casting.
Implicit Casting (Safe Conversion)
Automatically converts a smaller type to a larger type:
int num = 10;
double result = num; // Implicit cast
Explicit Casting (Type Conversion)
Manually converts a larger type to a smaller type:
double num = 9.78;
int result = (int)num; // Explicit cast
Convert Class
For additional control, use the Convert class:
string numStr = "25";
int num = Convert.ToInt32(numStr);
9. String Interpolation
You can combine strings and variables using $:
string name = "Alice";
int age = 25;
Console.WriteLine($"Name: {name}, Age: {age}");
10. Example Program Using Variables
using System;
class Program
{
static void Main(string[] args)
{
int age = 30;
double salary = 50000.50;
string name = "John Doe";
bool isEmployed = true;
Console.WriteLine($"Name: {name}, Age: {age}, Salary: ${salary}, Employed: {isEmployed}");
}
}
C# variables and data types offer flexibility, ensuring you can efficiently work with data. Let me know if you’d like further examples or advanced topics, such as custom classes or collections! 😊
