Bird
Raised Fist0
C Sharp (C#)programming~5 mins

Type conversion and casting in C Sharp (C#) - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is the difference between implicit and explicit type conversion in C#?
Implicit conversion happens automatically when there is no risk of data loss, like converting an int to a double. Explicit conversion (casting) requires you to specify the type because it might lose data or cause errors, like converting a double to an int.
Click to reveal answer
beginner
How do you perform explicit casting from a double to an int in C#?
You use parentheses with the target type before the value, like this: int number = (int)3.14;. This cuts off the decimal part and keeps only the whole number.
Click to reveal answer
intermediate
What happens if you try to cast an object to an incompatible type using (Type) casting?
You get an InvalidCastException at runtime because the object cannot be converted to that type. You should check the type before casting or use safe casting methods.
Click to reveal answer
intermediate
What is the difference between as operator and regular casting in C#?
The as operator tries to cast an object to a type and returns null if it fails, instead of throwing an exception. Regular casting throws an exception if the cast is invalid.
Click to reveal answer
intermediate
Explain boxing and unboxing in C#.
Boxing is converting a value type (like int) to an object type, wrapping the value inside an object. Unboxing extracts the value type back from the object. Boxing and unboxing can affect performance if overused.
Click to reveal answer
Which of these conversions is implicit in C#?
Adouble to int
Bobject to string
Cint to double
Dstring to int
What does this code do? int x = (int)5.99;
AConverts 5.99 to 5 by dropping the decimal
BThrows an error
CRounds 5.99 to 6
DStores 5.99 as an int
What will happen if you cast an object to an incompatible type using (Type) casting?
AThrows InvalidCastException
BConverts silently
CReturns null
DReturns default value
Which operator returns null instead of throwing an exception when casting fails?
Acast operator (Type)
Bas operator
Cis operator
Dtypeof operator
What is boxing in C#?
AConverting a reference type to a value type
BConverting a string to a number
CCasting between two reference types
DConverting a value type to an object
Describe how implicit and explicit type conversions work in C# and when you should use each.
Think about when the computer converts types automatically versus when you must tell it to convert.
You got /4 concepts.
    Explain boxing and unboxing in C# and why they might affect program performance.
    Consider how value types become objects and back.
    You got /5 concepts.

      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

      1. Step 1: Understand casting in C#

        Casting uses parentheses and works only when types are compatible, like int to double or object to string.
      2. Step 2: Understand conversion methods

        Conversion methods like Convert.ToInt32 or int.Parse are safer and can handle strings and other types more flexibly.
      3. Final Answer:

        Casting requires compatible types and uses parentheses, conversion can be safer and more flexible. -> Option C
      4. 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

      1. Step 1: Recall casting syntax in C#

        Casting uses parentheses with the target type before the variable, like (int)d.
      2. 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.
      3. Final Answer:

        int i = (int)d; -> Option A
      4. 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

      1. Step 1: Understand casting from double to int

        Casting double to int truncates the decimal part, it does not round.
      2. Step 2: Apply casting to value 9.7

        Truncating 9.7 results in 9, so i = 9.
      3. Final Answer:

        9 -> Option D
      4. 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

      1. 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.
      2. Step 2: Analyze string "123abc"

        String contains non-digit characters, so conversion fails and throws FormatException at runtime.
      3. Final Answer:

        FormatException at runtime due to invalid string format -> Option A
      4. 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

      1. 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.
      2. Step 2: Analyze safe pattern

        Using pattern matching with 'is' checks type safely and assigns value or default 0 if not int, avoiding exceptions.
      3. Final Answer:

        int i = obj is int val ? val : 0; -> Option B
      4. 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 unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard