Variables and Data Types in C#
# 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
varkeyword for implicit typing.
-
Create constants using
const.
- Perform Type Casting (Type Conversion).
3. Declaring Variables
Syntax:dataType variableName = value;
4. Value Types vs. Reference Types
This is a critical concept in .NET architecture:- 1. Value Types (Stored on the Stack):
int, double, bool, and char. They hold their actual data directly. If you pass them to another variable, a pure copy is made.
- 2. Reference Types (Stored on the Heap):
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.
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.
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.
2. Explicit Casting (Manual): Converting a larger type to a smaller size. You must do this manually because data (like decimals) might be lost.
3. Type Conversion Methods:
C# provides a built-in Convert class to easily parse data types, especially useful for converting text into numbers.
8. Common Mistakes
-
Confusing
float,double, anddecimal:
-
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
varwhen 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
decimalfor currency calculations, neverdouble, to avoid rounding errors.
10. Exercises
-
1.
Declare variables for your age, height in meters (double), and whether you like coffee (bool). Print them out using
Console.WriteLine().
-
2.
Declare a
doublevariable with the value15.99. Use explicit casting to convert it to anintand print it.
11. MCQs with Answers
Which data type is used to store true or false values?
What is the difference between a string and a char?
Which of the following is a Value Type?
Where are Reference Types stored in memory?
What does the const keyword do?
What happens in int x = (int)5.99;?
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)
Which data type is best for storing financial/money values?
What suffix must be added to a number to make it a float?
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
decimalinstead ofdoublefor 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.