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

Enum vs constants decision in C Sharp (C#) - Quick Revision & Key Differences

Choose your learning style9 modes available
Recall & Review
beginner
What is an enum in C#?
An enum (short for enumeration) is a special data type that lets you define a set of named constant values. It helps group related constants under one type for better readability and type safety.
Click to reveal answer
beginner
What are constants in C#?
Constants are fixed values declared with the keyword const. They cannot change during program execution and are usually used for single fixed values like numbers or strings.
Click to reveal answer
intermediate
When should you use an enum instead of constants?
Use enums when you have a group of related named values that represent a category or set of options. Enums improve code clarity and help prevent invalid values by restricting inputs to the defined set.
Click to reveal answer
intermediate
When are constants preferred over enums?
Use constants when you need a single fixed value or unrelated fixed values. Constants are simpler and better for values like mathematical constants or configuration strings.
Click to reveal answer
intermediate
What is a key advantage of enums over constants?
Enums provide type safety, meaning the compiler can check if you use only valid values from the enum. Constants do not provide this safety since they are just individual values.
Click to reveal answer
Which of the following is true about enums in C#?
AEnums are used for unrelated single fixed values.
BEnums group related named constants under one type.
CEnums can change their values during runtime.
DEnums cannot be used with switch statements.
When should you use a constant instead of an enum?
AWhen you have multiple related named values.
BWhen you want to restrict inputs to a set.
CWhen you need a single fixed value like Pi.
DWhen you want type safety for a set of options.
What keyword is used to declare a constant in C#?
Aconst
Benum
Cstatic
Dreadonly
Which feature is a benefit of enums over constants?
AThey provide type safety.
BThey can be changed at runtime.
CThey are faster to access.
DThey use less memory.
Can enums be used in switch statements in C#?
ANo, enums cannot be used in switch statements.
BOnly if the enum is declared as public.
COnly if the enum values are integers.
DYes, enums work well with switch statements.
Explain when you would choose an enum over constants in C# and why.
Think about grouping and safety.
You got /4 concepts.
    Describe a scenario where constants are better than enums and explain your reasoning.
    Consider simple fixed values like Pi.
    You got /4 concepts.