C# Variables and Data Types — Complete Beginner Guide
Learning Objectives
- Declare and assign variables in C#
- Choose among int, double, bool, and string
- Understand value ranges and defaults
- Print variables and combine them safely
- Avoid naming and type mismatch errors
What Is a Variable
A variable is a named storage location in memory. You create one by writing a type, a name, and a value. C# is statically typed, so the type is fixed when you declare the variable.
int age = 25;
double price = 9.99;
bool isActive = true;
string name = "Alice";
After declaration, age always holds an integer unless you change its value to another integer.
Core Value Types
Beginners use a small set of types for most early programs.
| Type | Stores | Example |
|---|---|---|
| int | Whole numbers | 42 |
| double | Decimal numbers | 3.14 |
| bool | true or false | true |
| text string | Sequences of characters | Hello |
int for whole numbers
The int type stores whole numbers roughly from negative two billion to positive two billion. Use it for counts, ages, IDs, and loop counters.
int score = 100;
int players = 4;
Console.WriteLine(score + players);
double for decimals
The double type stores floating point numbers with about fifteen digits of precision. Use it for prices, measurements, and scientific values.
double temperature = 36.6;
double balance = 1250.75;
Console.WriteLine(temperature);
bool for true or false
The bool type holds only two values: true or false. Conditions and flags use bool.
bool isRaining = false;
bool hasTicket = true;
Console.WriteLine(isRaining);
string for text
The string type holds text. Values appear inside double quotes.
string greeting = "Good morning";
string city = "Mumbai";
Console.WriteLine(greeting + " from " + city);
Declaring and Assigning
You can declare first and assign later, or do both in one line.
int count;
count = 10;
int total = 5;
total = total + 3;
Console.WriteLine(total);
Once a variable holds a value you can update it, as long as the new value matches the declared type.
Constants and Immutability Preview
Some values should never change. The const keyword creates a compile time constant.
const double Pi = 3.14159;
Console.WriteLine(Pi);
Trying to reassign Pi causes a compile error. You will explore constants further in the next lesson.
Default Values
A declared local variable must be assigned before use. Fields inside classes receive default values automatically, such as zero for numbers and false for bool.
int x;
// Console.WriteLine(x); // Not allowed yet
x = 0;
Console.WriteLine(x);
This rule prevents programs from reading memory that was never set.
Common Beginner Mistakes
Wrong type for the data
int price = 9.99; // Error: cannot convert double to int automatically
Use double for decimals, or convert on purpose when you truly want a whole number.
Forgetting quotes on strings
string name = Alice; // Error
string name2 = "Alice"; // Correct
Bad variable names
Names must start with a letter or underscore and cannot include spaces. Prefer clear names such as studentAge over x1.
Mixing types in output
int a = 5;
string b = "5";
Console.WriteLine(a); // Prints 5 as a number
Console.WriteLine(b); // Prints 5 as text
Numbers can be calculated; text cannot be added mathematically without conversion.
Practice Snippet
Combine the ideas in one short program:
string playerName = "Ravi";
int level = 7;
double highScore = 98.5;
bool isOnline = true;
Console.WriteLine(playerName);
Console.WriteLine(level);
Console.WriteLine(highScore);
Console.WriteLine(isOnline);
Run it and confirm each value prints correctly.
Key Takeaways
💡
- Variables pair a type, a name, and a value
- Use int for whole numbers and double for decimals
- bool holds true or false; string holds text in quotes
- C# is statically typed, so types must match on assignment
- Constants created with const never change after definition
Practice Questions
- Which type stores whole numbers such as 42?
- What two values can a bool variable hold?
- Why must a string value sit inside double quotes?
- What happens if you assign 3.5 to an int variable without conversion?
- Declare four variables for a name, age, GPA, and membership flag, then print them.
Extended Learning: Beyond the Basics
Key Concepts Summary
This lesson covers fundamental concepts that serve as building blocks for more advanced topics. Master these basics before moving to intermediate and advanced material.
Prerequisite Knowledge
Before studying this topic in depth, ensure you understand:
- Basic definitions and terminology
- Core formulas and their derivations
- Common applications in real-world scenarios
- Typical pitfalls and misconceptions
Step-by-Step Walkthrough
Let us work through a complete example to solidify understanding:
Step 1: Identify the problem type. Determine which category this problem falls into and which approach is most appropriate.
Step 2: Apply the relevant formula. Substitute known values into the appropriate equation, paying attention to units and constraints.
Step 3: Simplify and solve. Work through the algebra step by step, checking each intermediate result.
Step 4: Verify the answer. Check that the solution satisfies the original conditions and makes logical sense.
Common Mistakes to Avoid
- Misidentifying the problem type — Always start by categorizing the problem
- Incorrect formula application — Verify the formula applies to your specific case
- Unit errors — Always check units are consistent throughout
- Sign errors — Pay careful attention to positive and negative values
- Rounding too early — Keep full precision until the final answer
- Forgetting constraints — Check that solutions satisfy all given conditions
Real-World Applications
Understanding this concept is essential for:
- Advanced problem solving in examinations
- Building intuition for more complex topics
- Developing analytical thinking skills
- Applying mathematical reasoning to practical situations
Practice Exercises
- Basic: Solve 5 problems of increasing difficulty using the core concept.
- Intermediate: Apply the concept to word problems with multiple steps.
- Advanced: Combine this concept with 2-3 other topics in a comprehensive problem.
- Challenge: Create your own problem that requires this concept plus additional knowledge.
Interview and Exam Tips
- Common question patterns: Identify which exam or interview formats test this topic
- Time management: Practice completing problems within time constraints
- Common traps: Know the typical mistakes examiners set as distractors
- Verification strategy: Develop a quick method to check your answer
Related Topics
- Previous Topic — Build on the foundation laid here
- Next Topic — See how this concept extends to more advanced material
- Practice Problems — Additional exercises with solutions
- Video Explanation — Visual walkthrough of key concepts
Quick Reference Card
| Concept | Formula/Rule | Key Insight | Common Use |
|---|---|---|---|
| Core Definition | See main text | Understand the intuition | Foundation for all applications |
| Key Formula | See main text | Memorize the derivation | Direct problem solving |
| Special Case | See main text | Recognize when it applies | Simplifies complex problems |
| Edge Case | See main text | Know the limitations | Avoids errors |
Further Reading
For students who want to go deeper:
- Standard textbooks on the subject
- Research papers applying this concept
- Online courses with video explanations
- Practice problem collections with detailed solutions