Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9146065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:40:08+00:00 2026-06-17T10:40:08+00:00

C# enum values are not limited to only values listed in it’s definition and

  • 0

C# enum values are not limited to only values listed in it’s definition and may store any value of it’s base type. If base type is not defined than Int32 or simply int is used.

I am developing a WCF serivice which needs to be confident that some enum has a value assigned as opposed to the default value for all enums of 0. I start with a unit test to find out whether [Required] would do right job here.

using System.ComponentModel.DataAnnotations;
using Xunit;

public enum MyEnum
{
    // I always start from 1 in order to distinct first value from the default value
    First = 1,
    Second,
}

public class Entity
{
    [Required]
    public MyEnum EnumValue { get; set; }
}

public class EntityValidationTests
{
    [Fact]
    public void TestValidEnumValue()
    {
        Entity entity = new Entity { EnumValue = MyEnum.First };

        Validator.ValidateObject(entity, new ValidationContext(entity, null, null));
    }

    [Fact]
    public void TestInvalidEnumValue()
    {
        Entity entity = new Entity { EnumValue = (MyEnum)(-126) };
        // -126 is stored in the entity.EnumValue property

        Assert.Throws<ValidationException>(() =>
            Validator.ValidateObject(entity, new ValidationContext(entity, null, null)));
    }
}

It does not, the second test does not throw any exception.

My question is: is there a validator attribute to check that supplied value is in Enum.GetValues?

Update.
Make sure to use the ValidateObject(Object, ValidationContext, Boolean) with last parameter equal to True instead of ValidateObject(Object, ValidationContext) as done in my unit test.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T10:40:09+00:00Added an answer on June 17, 2026 at 10:40 am

    There’s EnumDataType in .NET4+ …

    Make sure you set the 3rd parameter, validateAllProperties=true in the call to ValidateObject

    so from your example:

    public class Entity
    {
        [EnumDataType(typeof(MyEnum))]
        public MyEnum EnumValue { get; set; }
    }
    
    [Fact]
    public void TestInvalidEnumValue()
    {
        Entity entity = new Entity { EnumValue = (MyEnum)(-126) };
        // -126 is stored in the entity.EnumValue property
    
        Assert.Throws<ValidationException>(() =>
            Validator.ValidateObject(entity, new ValidationContext(entity, null, null), true));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a set of enum values defined in the Foo class (below). namespace
I want to store additional information in my Enum values and therefore came up
I have a base class that has an enum value that I am using
Why is it not possible to use enum values as strings in a switch
For me, the classic wisdom is to store enum values (OrderStatus, UserTypes, etc) as
I want to restrict input values like this <simpleType name=SomeCode> <restriction base=string> <enumeration value=036222B/>
If I bind a WinForms ComboBox to an enum type's values, i.e. combo1.DropDownStyle =
I'm using Windows Forms and VS2008. I want to store an enum value in
I added a new value (NULL_IS_NOT_NIL) to an enum in ksql.h enum { PKEY
I need to display all enum values as tab items on TabControl, except one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.