Ad Code

Responsive Advertisement

Data Types 01: How to check count of values in enum parameter

Data Types 01: How to check count of values in enum parameter ?

To check the count of values in an enum like SpeedModeStatus, you can use the Enum.GetValues method to get all the values in the enum and then use Length or Count to find the total number of values.

Here is a quick example in C#:
using System;

public class Program
{
    public enum SpeedModeStatus
    {
        None = 0,
        Xtelth = 1,
        Xtride = 2,
        Xonic = 3,
    }

    public static void Main()
    {
        // Get the count of enum values
        int count = Enum.GetValues(typeof(SpeedModeStatus)).Length;
        
        // Output the count
        Console.WriteLine("Number of SpeedModeStatus values: " + count);
    }
}


Explanation:
Enum.GetValues(typeof(SpeedModeStatus)) retrieves all the values defined in the SpeedModeStatus enum.
.Length gives the total count of these values.

In this case, the output will be:
Number of SpeedModeStatus values: 4

This includes all defined values, even if some have the same numeric value.

Post a Comment

0 Comments

Ad Code

Responsive Advertisement