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

Enum vs constants decision in C Sharp (C#) - Hands-On Comparison

Choose your learning style9 modes available
Enum vs Constants Decision in C#
📖 Scenario: You are building a simple program to manage the status of orders in a store. Each order can have a status like Pending, Shipped, or Delivered.You want to decide whether to use constants or an enum to represent these statuses in your code.
🎯 Goal: Create a program that first uses constants to represent order statuses, then switches to using an enum. Finally, print the status of an order using the enum.
📋 What You'll Learn
Create constants for order statuses with exact names and values
Create an enum for order statuses with exact names
Declare a variable with an enum value
Print the enum value to show the order status
💡 Why This Matters
🌍 Real World
Enums and constants are used in many programs to represent fixed sets of options, like order statuses, user roles, or colors.
💼 Career
Understanding when to use enums or constants is important for writing clear, maintainable code in professional software development.
Progress0 / 4 steps
1
Create constants for order statuses
Create three public constants called Pending, Shipped, and Delivered inside a public static class OrderStatusConstants. Assign the string values "Pending", "Shipped", and "Delivered" respectively.
C Sharp (C#)
Need a hint?

Use public const string inside a static class to create constants.

2
Create an enum for order statuses
Create a public enum called OrderStatus with the members Pending, Shipped, and Delivered.
C Sharp (C#)
Need a hint?

Use public enum OrderStatus and list the status names separated by commas.

3
Declare a variable with an enum value
Declare a variable called currentStatus of type OrderStatus and set it to OrderStatus.Shipped.
C Sharp (C#)
Need a hint?

Use the enum type OrderStatus and assign the member OrderStatus.Shipped to the variable.

4
Print the enum value
Write a Console.WriteLine statement to print the value of currentStatus.
C Sharp (C#)
Need a hint?

Use Console.WriteLine(currentStatus); inside a Main method to print the enum value.