When learning any programming language, mastering variables, data types, and operators is essential. These are the fundamental tools you’ll use to store information, perform calculations, and control your application’s logic.
In C#, the syntax is clean and strongly typed, meaning you must declare a variable’s type before using it — making your code more reliable and predictable.
In this post, we’ll break down how to declare and use variables, explore C#’s most common data types, and look at the powerful set of operators available to you.
📦 What Are Variables?
A variable is like a labeled box in memory where you store information.
🔹 Declaring a Variable in C#
int score = 100;
string name = "Alice";
bool isGameOver = false;
In C#, the syntax for declaring a variable is:
<type> <variableName> = <value>;
📚 Common C# Data Types
C# is statically typed, so you must define the type of data your variable will hold.
| Type | Description | Example |
|---|---|---|
int | Whole numbers | int age = 30; |
double | Decimal numbers | double temp = 36.5; |
decimal | High-precision decimals (e.g., for money) | decimal price = 19.99m; |
bool | True or false values | bool isAlive = true; |
string | Text | string name = "Bob"; |
char | A single character | char grade = 'A'; |
🧠 Note:
decimalvalues require anmsuffix, e.g.,19.99m.
🧠 Variable Naming Rules
- Names must begin with a letter or underscore.
- Cannot contain spaces or special characters (except
_). - C# is case-sensitive (
scoreandScoreare different). - Use camelCase for variable names by convention.
✅ Example:
int playerScore = 0;
string firstName = "John";
🔧 Operators in C#
Operators let you perform operations like addition, comparison, and logic.
➕ Arithmetic Operators
| Operator | Meaning | Example |
|---|---|---|
+ | Addition | a + b |
- | Subtraction | a - b |
* | Multiplication | a * b |
/ | Division | a / b |
% | Modulus (remainder) | a % b |
int a = 10;
int b = 3;
int result = a + b; // 13
🔁 Comparison Operators
Used in conditions (if, loops, switch etc.):
| Operator | Meaning |
|---|---|
== | Equal to |
!= | Not equal to |
> | Greater than |
< | Less than |
>= | Greater than or equal |
<= | Less than or equal |
if (score >= 100)
{
Console.WriteLine("Level complete!");
}
⚙️ Logical Operators
Used for combining conditions:
| Operator | Description | Example |
|---|---|---|
&& | Logical AND | if (isLoggedIn && isAdmin) |
| ` | ` | |
! | Logical NOT | if (!isComplete) |
🧪 Practice Example
int age = 25;
string name = "Sophie";
bool isMember = true;
if (age >= 18 && isMember)
{
Console.WriteLine(name + " has access.");
}
else
{
Console.WriteLine("Access denied.");
}
🧠 Tips for Beginners
- Always choose the right data type — using
decimalfor currency, for example. - Use meaningful variable names — they make your code easier to read.
- Practice combining operators in
ifstatements to control program flow.
💬 Need Help?
Have questions about your code or unsure which data type to use? Drop us a message on our Contact Page — we’re happy to help!