Skip to main content

requiredenum by Yaroslav

NuGet / site data

Nuget GitHub last commit GitHub Repo stars

Details

Info

info

Name: requiredenum

RequiredEnum is an open-source analyzer that helps handle all enum values for switch.

Author: Yaroslav

NuGet: https://www.nuget.org/packages/requiredenum/

You can find more details at https://github.com/emptycoder/RequiredEnum

Source: https://github.com/emptycoder/RequiredEnum

Author

note

Yaroslav Alt text

Original Readme

note

RequiredEnum

RequiredEnum is an open-source analyzer that helps handle all enum values for switch.

Installation

NuGet: dotnet add package requiredenum

Usage

Just add 'Required' prefix for any name of enum in your project and you will get the error when one of cases weren't handle in a switch statement.

var test = (RequiredNumbers) Random.Shared.Next(0, Enum.GetNames(typeof(RequiredNumbers)).Length);
switch (test)
{
case RequiredNumbers.Zero:
break;
case RequiredNumbers.One:
break;
default:
throw new ArgumentOutOfRangeException();
}

internal enum RequiredNumbers
{
Zero,
One,
Two
}

This code will throw the error ('Two' case wasn't handle) and it can't be compiled.

License

RequiredEnum distributed under MIT license.

About

note

Raise an error at compile time if not have switch handle case all enums values

How to use

Example (source csproj, source files)

This is the CSharp Project that references requiredenum

<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="RequiredEnum" Version="0.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>


</Project>

Generated Files

Useful

Download Example (.NET C#)

Share requiredenum

https://ignatandrei.github.io/RSCG_Examples/v2/docs/requiredenum

Category "Enum" has the following generators:

1 CredFetoEnum

2 EnumClass

3 EnumsEnhanced

4 EnumUtilities

5 Flaggen

6 FusionReactor

7 Genbox.FastEnum

8 jos.enumeration

9 NetEscapades.EnumGenerators

10 PMart.Enumeration

11 requiredenum

See category

Enum