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

TryParse for safe conversion 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 safely convert a string to an integer using TryParse.

C Sharp (C#)
int number;
bool success = int.[1]("123", out number);
Drag options to blanks, or click blank then click option'
ATryParse
BToInt32
CParse
DConvert
Attempts:
3 left
💡 Hint
Common Mistakes
Using Parse which throws an exception on failure.
Using Convert which throws exceptions for invalid input.
2fill in blank
medium

Complete the code to check if the string "456" can be converted to an integer.

C Sharp (C#)
if (int.[1]("456", out int result))
{
    Console.WriteLine("Conversion succeeded: " + result);
}
Drag options to blanks, or click blank then click option'
AToInt32
BConvert
CParse
DTryParse
Attempts:
3 left
💡 Hint
Common Mistakes
Using Parse which throws exceptions instead of returning a boolean.
Not using the out keyword for the result parameter.
3fill in blank
hard

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

C Sharp (C#)
double value;
bool success = double.[1]("12.34", out value);
Drag options to blanks, or click blank then click option'
AToDouble
BTryParse
CConvert
DParse
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting the out keyword causes a compile error.
Using Parse which throws exceptions on invalid input.
4fill in blank
hard

Fill both blanks to create a dictionary with word lengths only for words longer than 4 characters.

C Sharp (C#)
var words = new List<string> { "apple", "cat", "banana", "dog" };
var lengths = new Dictionary<string, int> { { "length", 0 } };
lengths = words.ToDictionary(word => word, word => word.[1]);
var filtered = lengths.Where(kv => kv.Value [2] 4).ToDictionary(kv => kv.Key, kv => kv.Value);
Drag options to blanks, or click blank then click option'
ALength
BCount
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using Count which is not a string property.
Using < instead of > causing wrong filtering.
5fill in blank
hard

Fill all three blanks to create a dictionary of uppercase words and their lengths, filtering only words longer than 3 characters.

C Sharp (C#)
var words = new List<string> { "tree", "sun", "flower", "sky" };
var result = words.Where(word => word.[3] [1] 3)
                  .ToDictionary(word => word.[2](), word => word.[3]);
Drag options to blanks, or click blank then click option'
ALength
B>
CToUpper
DCount
Attempts:
3 left
💡 Hint
Common Mistakes
Using Count instead of Length for string length.
Not using parentheses for ToUpper method call.
Using < instead of > in the filter.