📖 Scenario: You are working on a simple program that handles different types of numbers. Sometimes you need to convert numbers from one type to another to make calculations or display results correctly.
🎯 Goal: Build a program that converts and casts numbers between int, double, and string types, demonstrating how to change data types safely and correctly.
📋 What You'll Learn
Create an integer variable with a specific value
Create a double variable with a specific value
Convert the integer to a double using casting
Convert the double to an integer using casting
Convert the integer to a string using type conversion
Print all converted values to show the results
💡 Why This Matters
🌍 Real World
Type conversion and casting are common when working with user input, calculations, and displaying data in different formats.
💼 Career
Understanding how to convert and cast types is essential for software developers to avoid errors and write clear, correct code.
Progress0 / 4 steps
1
Create initial number variables
Create an int variable called myInt and set it to 42. Create a double variable called myDouble and set it to 3.14.
C Sharp (C#)
Hint
Use int myInt = 42; and double myDouble = 3.14; to create the variables.
2
Add variables for casting and conversion
Create a double variable called castedDouble and set it by casting myInt to double. Create an int variable called castedInt and set it by casting myDouble to int.
C Sharp (C#)
Hint
Use (double)myInt to cast int to double and (int)myDouble to cast double to int.
3
Convert integer to string
Create a string variable called intAsString and set it by converting myInt to a string using the ToString() method.
C Sharp (C#)
Hint
Use myInt.ToString() to convert the integer to a string.
4
Print all converted values
Print the values of castedDouble, castedInt, and intAsString each on a new line using Console.WriteLine().
C Sharp (C#)
Hint
Use Console.WriteLine() to print each variable on its own line.
Practice
(1/5)
1. What is the main difference between type conversion and casting in C#?
easy
A. Casting changes data to string only, conversion changes data to numbers only.
B. Casting is automatic, conversion always requires manual code.
C. Casting requires compatible types and uses parentheses, conversion can be safer and more flexible.
D. Casting works only with classes, conversion works only with structs.
Solution
Step 1: Understand casting in C#
Casting uses parentheses and works only when types are compatible, like int to double or object to string.
Step 2: Understand conversion methods
Conversion methods like Convert.ToInt32 or int.Parse are safer and can handle strings and other types more flexibly.
Final Answer:
Casting requires compatible types and uses parentheses, conversion can be safer and more flexible. -> Option C
Quick Check:
Casting vs Conversion = Casting requires compatible types and uses parentheses, conversion can be safer and more flexible. [OK]
Hint: Casting uses parentheses; conversion uses methods like Convert.ToInt32 [OK]
Common Mistakes:
Thinking casting works with all types automatically
Confusing conversion methods with casting syntax
Assuming casting changes data format like conversion
2. Which of the following is the correct syntax to cast a double variable d to an int in C#?
easy
A. int i = (int)d;
B. int i = int(d);
C. int i = Convert.ToInt(d);
D. int i = d as int;
Solution
Step 1: Recall casting syntax in C#
Casting uses parentheses with the target type before the variable, like (int)d.
Step 2: Check each option
int i = (int)d; uses correct casting syntax. int i = int(d); is invalid syntax. int i = Convert.ToInt(d); uses wrong method name (should be Convert.ToInt32). int i = d as int; uses 'as' which works only with reference types.
Final Answer:
int i = (int)d; -> Option A
Quick Check:
Correct cast syntax = int i = (int)d; [OK]
Hint: Casting uses (type)variable syntax, not functions or 'as' keyword [OK]
Common Mistakes:
Using 'as' keyword for value types
Calling non-existent Convert.ToInt method
Using function-like syntax for casting
3. What will be the output of the following C# code?
double d = 9.7;
int i = (int)d;
Console.WriteLine(i);
medium
A. Compilation error
B. 10
C. 9.7
D. 9
Solution
Step 1: Understand casting from double to int
Casting double to int truncates the decimal part, it does not round.
Step 2: Apply casting to value 9.7
Truncating 9.7 results in 9, so i = 9.
Final Answer:
9 -> Option D
Quick Check:
Double to int cast truncates decimals = 9 [OK]
Hint: Casting double to int drops decimals, no rounding [OK]
Common Mistakes:
Assuming casting rounds the number
Expecting a compilation error
Confusing output with original double value
4. Identify the error in this C# code snippet:
string s = "123abc";
int num = Convert.ToInt32(s);
Console.WriteLine(num);
medium
A. FormatException at runtime due to invalid string format
B. num will be 0 because of invalid characters
C. Compilation error because Convert.ToInt32 does not exist
D. No error, code runs and prints 123
Solution
Step 1: Understand Convert.ToInt32 behavior
Convert.ToInt32 tries to convert the whole string to an integer and throws an exception if format is invalid.
Step 2: Analyze string "123abc"
String contains non-digit characters, so conversion fails and throws FormatException at runtime.
Final Answer:
FormatException at runtime due to invalid string format -> Option A
Quick Check:
Invalid string format causes FormatException [OK]
Hint: Convert.ToInt32 throws error if string has non-digit chars [OK]
Common Mistakes:
Assuming partial conversion succeeds
Expecting zero instead of exception
Thinking Convert.ToInt32 is compile-time error
5. You have an object obj that holds an integer value. Which code safely converts obj to an int without risking an exception if obj is not an int?
hard
A. int i = Convert.ToInt32(obj);
B. int i = obj is int val ? val : 0;
C. int i = obj as int;
D. int i = (int)obj;
Solution
Step 1: Understand casting and conversion risks
Direct cast (int)obj throws exception if obj is not int. Convert.ToInt32 may throw exceptions too. 'as' cannot be used with value types like int.
Step 2: Analyze safe pattern
Using pattern matching with 'is' checks type safely and assigns value or default 0 if not int, avoiding exceptions.
Final Answer:
int i = obj is int val ? val : 0; -> Option B
Quick Check:
Pattern matching safely converts or defaults = int i = obj is int val ? val : 0; [OK]
Hint: Use 'is' pattern matching to safely cast without exceptions [OK]
Common Mistakes:
Using 'as' with value types causing compile errors
Casting without checking type causing exceptions
Assuming Convert.ToInt32 always safe
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.