Created
June 15, 2024 14:36
-
-
Save ShutovKS/800d455ae572685a624b468cefbcc924 to your computer and use it in GitHub Desktop.
Getting random value from enum in C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class Extensions | |
{ | |
public static int RandomIndex<T>() where T : Enum | |
{ | |
var values = Enum.GetValues(typeof(T)); | |
return new System.Random().Next(values.Length); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment