Enum with Customized Value in C#

An enum (enumeration) in C# is used to store a set of named constants such as days of the week, months, seasons, or any fixed collection of related values. By default, enum constants start from 0 and increment by 1, but you can customize these values to match your specific requirements.

Customizing enum values is useful when you need specific numeric representations, want to maintain compatibility with external systems, or need non-sequential numbering.

Syntax

Following is the syntax for declaring an enum with customized values −

public enum EnumName {
    Value1 = customValue1,
    Value2 = customValue2,
    Value3  // continues from previous + 1
}

Using Sequential Custom Values

When you assign a custom value to the first enum member, subsequent members automatically increment from that value −

using System;

public class Demo {
    public enum Vehicle { Car = 20, Motorcycle, Bus, Truck }
    
    public static void Main() {
        int a = (int)Vehicle.Car;
        int b = (int)Vehicle.Motorcycle;
        int c = (int)Vehicle.Bus;
        int d = (int)Vehicle.Truck;
        
        Console.WriteLine("Car = {0}", a);
        Console.WriteLine("Motorcycle = {0}", b);
        Console.WriteLine("Bus = {0}", c);
        Console.WriteLine("Truck = {0}", d);
    }
}

The output of the above code is −

Car = 20
Motorcycle = 21
Bus = 22
Truck = 23

Using Non-Sequential Custom Values

You can assign specific values to individual enum members, creating non-sequential numbering −

using System;

public class Demo {
    public enum Priority { Low = 1, Medium = 5, High = 10, Critical = 100 }
    
    public static void Main() {
        Console.WriteLine("Low = {0}", (int)Priority.Low);
        Console.WriteLine("Medium = {0}", (int)Priority.Medium);
        Console.WriteLine("High = {0}", (int)Priority.High);
        Console.WriteLine("Critical = {0}", (int)Priority.Critical);
        
        // Using enum values in comparisons
        Priority taskPriority = Priority.High;
        if ((int)taskPriority >= 10) {
            Console.WriteLine("High priority task detected!");
        }
    }
}

The output of the above code is −

Low = 1
Medium = 5
High = 10
Critical = 100
High priority task detected!

Using Different Data Types

Enums can use different underlying data types like byte, short, long, etc. −

using System;

public class Demo {
    public enum Status : byte { Inactive = 0, Active = 1, Pending = 5, Archived = 255 }
    
    public static void Main() {
        Console.WriteLine("Inactive = {0}", (byte)Status.Inactive);
        Console.WriteLine("Active = {0}", (byte)Status.Active);
        Console.WriteLine("Pending = {0}", (byte)Status.Pending);
        Console.WriteLine("Archived = {0}", (byte)Status.Archived);
        
        // Check enum type
        Console.WriteLine("Underlying type: {0}", Enum.GetUnderlyingType(typeof(Status)));
    }
}

The output of the above code is −

Inactive = 0
Active = 1
Pending = 5
Archived = 255
Underlying type: System.Byte

Common Use Cases

Use Case Example Benefit
HTTP Status Codes OK = 200, NotFound = 404 Matches standard conventions
Database IDs Admin = 1, User = 2, Guest = 3 Consistent with database values
Bit Flags Read = 1, Write = 2, Execute = 4 Enables bitwise operations

Conclusion

Customizing enum values in C# allows you to assign specific numeric values to enum members instead of using the default sequential numbering. This is particularly useful for maintaining compatibility with external systems, implementing specific business logic, or creating meaningful numeric representations for your constants.

Updated on: 2026-03-17T07:04:35+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements