Skip to main content
C# Fundamentals for Beginners to Advanced
CHAPTER 04 Beginner

Variables and Data Types in C#

Updated: May 17, 2026
5 min read

# CHAPTER 4

Variables and Data Types

1. Introduction

To build useful programs, you need to store data in the computer's memory—like a player's score, a user's name, or a bank balance. A variable is a labeled box in memory. Because C# is a strongly typed language, you must tell the compiler exactly what *type* of data will go inside that box before you use it.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Declare and initialize variables.
  • Understand primitive data types (int, double, char, bool, string).
  • Differentiate between Value Types and Reference Types.
  • Use the var keyword for implicit typing.
  • Create constants using const.
  • Perform Type Casting (Type Conversion).

3. Declaring Variables

Syntax: dataType variableName = value;
csharp
12345
int age = 25;               // Integer (whole number)
double price = 19.99;       // Double (decimal number)
char grade = 'A';           // Single character (use single quotes)
string name = "John";       // Text (use double quotes)
bool isOnline = true;       // Boolean (true or false)

4. Value Types vs. Reference Types

This is a critical concept in .NET architecture:
  1. 1. Value Types (Stored on the Stack):
Types like int, double, bool, and char. They hold their actual data directly. If you pass them to another variable, a pure copy is made.
  1. 2. Reference Types (Stored on the Heap):
Types like string, arrays, and custom Classes. The actual data is stored in the Heap, while the variable on the Stack only holds a memory address (a reference) pointing to the Heap.

5. Constants (const)

If you have a value that should *never* change during the execution of your program, declare it as a constant. If you try to change it later, the compiler will stop you.
csharp
12
const double Pi = 3.14159;
// Pi = 4.0; // ERROR: The left-hand side of an assignment must be a variable

6. The var Keyword (Implicit Typing)

Introduced to make code cleaner, var allows you to omit the data type. The compiler will figure out the type automatically by looking at the value on the right side of the equals sign.
csharp
12345678910
// Explicit typing
string firstName = "Alice";
int level = 5;

// Implicit typing (var)
var lastName = "Smith"; // Compiler knows this is a string
var speed = 10.5;       // Compiler knows this is a double

// ERROR: You cannot use var without assigning a value immediately!
// var health; 

7. Type Casting (Type Conversion)

Sometimes you need to convert data from one type to another.

1. Implicit Casting (Automatic): Converting a smaller type to a larger type size. No data is lost.

csharp
12
int myInt = 9;
double myDouble = myInt; // Automatic cast: 9 becomes 9.0

2. Explicit Casting (Manual): Converting a larger type to a smaller size. You must do this manually because data (like decimals) might be lost.

csharp
12
double myDouble = 9.78;
int myInt = (int)myDouble; // Manual cast: 9.78 becomes 9 (decimal is chopped off)

3. Type Conversion Methods: C# provides a built-in Convert class to easily parse data types, especially useful for converting text into numbers.

csharp
12
int myInt = 10;
string myString = Convert.ToString(myInt); // Converts 10 to "10"

8. Common Mistakes

  • Confusing float, double, and decimal:
  • double: Default for decimals. (e.g., double x = 1.5;)
  • float: Requires an 'f' suffix. Saves memory, less precise. (e.g., float x = 1.5f;)
  • decimal: Requires an 'm' suffix. Extremely precise, used for financial/money data. (e.g., decimal money = 19.99m;)
  • Single vs. Double Quotes: Characters (char) require single quotes ('A'). Strings (string) require double quotes ("Hello").

9. Best Practices

  • Use var when the type is obvious from the right side of the assignment (e.g., var name = "Bob";). Don't use it if the return type is ambiguous (e.g., var result = Calculate();).
  • Always use decimal for currency calculations, never double, to avoid rounding errors.

10. Exercises

  1. 1. Declare variables for your age, height in meters (double), and whether you like coffee (bool). Print them out using Console.WriteLine().
  1. 2. Declare a double variable with the value 15.99. Use explicit casting to convert it to an int and print it.

11. MCQs with Answers

Question 1

Which data type is used to store true or false values?

Question 2

What is the difference between a string and a char?

Question 3

Which of the following is a Value Type?

Question 4

Where are Reference Types stored in memory?

Question 5

What does the const keyword do?

Question 6

What happens in int x = (int)5.99;?

Q7. Is this valid code? var x; x = 10; a) Yes b) No Answer: b) No (var variables must be initialized on the same line so the compiler can deduce the type)
Question 8

Which data type is best for storing financial/money values?

Question 9

What suffix must be added to a number to make it a float?

Question 10

What does Convert.ToString(45) do?

12. Interview Questions

  • Q: Explain the difference between Value Types and Reference Types in C# memory management.
  • Q: Why should you use decimal instead of double for financial calculations?
  • Q: What is Implicit vs. Explicit type casting?

13. Summary

Variables store data in memory and require specific types. Value types (int, bool) live on the Stack, while Reference types (string) live on the Heap. The var keyword lets the compiler deduce the type, const locks a variable's value, and Casting allows safe conversion between different types.

14. Next Chapter Recommendation

Now that we have data stored in memory, we need to manipulate it. In Chapter 5: Operators in C#, we will learn how to perform mathematics, comparisons, and logic on our variables.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·