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

Enum parsing from strings in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Enum Parsing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Enum parsing code?

Consider the following C# code that parses a string into an enum value. What will be printed?

C Sharp (C#)
using System;

enum Color { Red, Green, Blue }

class Program {
    static void Main() {
        string input = "Green";
        if (Enum.TryParse<Color>(input, out var color)) {
            Console.WriteLine((int)color);
        } else {
            Console.WriteLine("Parse failed");
        }
    }
}
A1
B0
CParse failed
D2
Attempts:
2 left
💡 Hint

Remember that enum values start at 0 by default and increment by 1.

Predict Output
intermediate
2:00remaining
What happens if parsing fails without TryParse?

What will happen when running this code?

C Sharp (C#)
using System;

enum Status { Active, Inactive }

class Program {
    static void Main() {
        string input = "Pending";
        var status = (Status)Enum.Parse(typeof(Status), input);
        Console.WriteLine(status);
    }
}
AThrows ArgumentException
BPrints 0
CPrints Pending
DPrints Active
Attempts:
2 left
💡 Hint

Enum.Parse throws an exception if the string does not match any enum name.

🔧 Debug
advanced
2:30remaining
Why does this Enum.TryParse fail unexpectedly?

Look at this code snippet. It tries to parse a string to an enum but always fails. Why?

C Sharp (C#)
using System;

enum Direction { North, South, East, West }

class Program {
    static void Main() {
        string input = "north";
        if (Enum.TryParse<Direction>(input, out var dir)) {
            Console.WriteLine(dir);
        } else {
            Console.WriteLine("Failed to parse");
        }
    }
}
AThe out variable dir is not declared correctly
BThe enum Direction does not contain "north" because it is misspelled
CEnum.TryParse is case-sensitive by default, so "north" does not match "North"
DEnum.TryParse requires a third boolean parameter to work
Attempts:
2 left
💡 Hint

TryParse can be case-insensitive if you specify an option.

📝 Syntax
advanced
2:00remaining
Which option correctly parses a string ignoring case?

Choose the code snippet that correctly parses the string "west" to the enum Direction ignoring case.

C Sharp (C#)
enum Direction { North, South, East, West }
AEnum.TryParse<Direction>("west", out var d);
BEnum.Parse(typeof(Direction), "west", false);
CEnum.Parse<Direction>("west", true);
DEnum.TryParse<Direction>("west", out var d, true);
Attempts:
2 left
💡 Hint

Look for the overload of TryParse that accepts a boolean for case-insensitivity.

🚀 Application
expert
3:00remaining
How many items are in the dictionary after parsing multiple enum strings?

Given this code, how many key-value pairs does the dictionary contain after execution?

C Sharp (C#)
using System;
using System.Collections.Generic;

enum Level { Low = 1, Medium = 2, High = 3 }

class Program {
    static void Main() {
        string[] inputs = { "Low", "Medium", "High", "low", "HIGH" };
        var dict = new Dictionary<Level, int>();
        foreach (var s in inputs) {
            if (Enum.TryParse<Level>(s, true, out var level)) {
                if (!dict.ContainsKey(level)) {
                    dict[level] = 0;
                }
                dict[level]++;
            }
        }
        Console.WriteLine(dict.Count);
    }
}
A5
B3
C2
D4
Attempts:
2 left
💡 Hint

Consider that parsing is case-insensitive and dictionary keys are unique enum values.