0
0
C Sharp (C#)programming~10 mins

Type conversion and casting in C Sharp (C#) - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to convert an integer to a double.

C Sharp (C#)
int number = 10;
double result = (double)[1];
Console.WriteLine(result);
Drag options to blanks, or click blank then click option'
AConvert
Bresult
C10
Dnumber
Attempts:
3 left
💡 Hint
Common Mistakes
Using the target variable name instead of the source variable.
Trying to cast a method or keyword instead of the variable.
2fill in blank
medium

Complete the code to convert a string to an integer using a built-in method.

C Sharp (C#)
string text = "123";
int number = [1](text);
Console.WriteLine(number);
Drag options to blanks, or click blank then click option'
AParse
BToString
CConvert.ToDouble
DToInt32
Attempts:
3 left
💡 Hint
Common Mistakes
Using ToString which converts to string, not integer.
Using Convert.ToDouble which converts to double, not int.
3fill in blank
hard

Fix the error in the code to safely convert a string to an integer using TryParse.

C Sharp (C#)
string input = "456";
if (int.[1](input, out int result))
{
    Console.WriteLine(result);
}
else
{
    Console.WriteLine("Conversion failed");
}
Drag options to blanks, or click blank then click option'
ATryParse
BToInt32
CConvert
DParse
Attempts:
3 left
💡 Hint
Common Mistakes
Using Parse which throws exceptions on failure.
Using Convert which does not have TryParse.
4fill in blank
hard

Fill both blanks to convert a double to an integer using casting and print the result.

C Sharp (C#)
double value = 9.78;
int intValue = ([1]) value;
Console.WriteLine([2]);
Drag options to blanks, or click blank then click option'
Aint
Bvalue
CintValue
Ddouble
Attempts:
3 left
💡 Hint
Common Mistakes
Casting to double instead of int.
Printing the original double variable instead of the casted int.
5fill in blank
hard

Fill all three blanks to create a dictionary with string keys and integer values, filtering only values greater than 10.

C Sharp (C#)
var data = new Dictionary<string, int> { {"a", 5}, {"b", 15}, {"c", 25} };
var filtered = data.Where(kv => kv.Value [1] 10)
               .ToDictionary(kv => kv.[2], kv => kv.[3]);
foreach (var item in filtered)
{
    Console.WriteLine($"{item.Key}: {item.Value}");
}
Drag options to blanks, or click blank then click option'
A>
BKey
CValue
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using less than operator which filters wrong entries.
Mixing up Key and Value in the dictionary creation.