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 7002787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:58:26+00:00 2026-05-27T20:58:26+00:00

A few days ago I asked a question titled How to constraint a generic

  • 0

A few days ago I asked a question titled How to constraint a generic to be of type enum?. To summarize the problem is the following code:

class MyClass<T> where T : enum // Not possible in C#
{
}

I was introduced to code contracts and that I could produce a compile time warning for the problem, which is all I want (to be informed at compile time that T should be an enum). I tried the follwing code (Full source).

class MyClass<T>
{
  public MyClass()
  {
    Contract.Requires(typeof(System.Enum).IsAssignableFrom(typeof(T)));
  }
}

It only produces a useless runtime error. I should be able to produce a compile time warning but I can’t get it to work. Can anyone tell me what I’m doing wrong?

Here’s a picture of project’s Code Contracts setting:
Code Contracts Setting

  • 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-05-27T20:58:27+00:00Added an answer on May 27, 2026 at 8:58 pm

    So, I wrote the following code in a file:

    public class SomeClass<T>
    {
        [ContractInvariantMethod]
        private void Invariants()
        {
            Contract.Invariant(typeof(System.Enum).IsAssignableFrom(typeof(T)));
        }
    
        /// <summary>Initializes a new instance of the SomeClass class.</summary>
        /// <param name="dependency"></param>
        public SomeClass()
        {
    
        }
    }
    
    public class SomeOtherClass
    {
        public SomeOtherClass()
        {
            var myClass = new SomeClass<int>();
    
        }
    }
    

    From there, I went into the Code Contracts section of the project options and checked all checkboxes under “static checking”. I then turned the warning level to “high”. When I rebuilt the solution, I received a warning: “Code Contracts: invariant requires unproven: typeof(…)” that corresponded to the class invariant.

    From there, I set the warning level back to low and saw that there was no warning, ala what you’re reporting. So, I think that setting the warning level to high is what you need.

    If that doesn’t work, you might try following what I did and defining your contract as a class invariant (which, pedantically, I would suggest doing anyway because this is more of a class level invariant, conceptually, than a result of your constructor’s execution).

    Edit: I saw your screenshot after I posted, so I’d amend this to suggest using my “if that doesn’t work” suggestion with the class level invariant instead of the requires invocation from the xtor.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A few days ago I asked the following question: How to draw graphics as
I asked a question a few days ago about how to customise the calendar
A few days ago I asked a question that might be a little unclear.
Guys I've asked few days ago a question and didn't have really time to
A few days ago i asked this question: Displaying images from Facebook photo albums
A few days ago I has asked a question on SO about helping me
I asked a pretty similar question a few days ago, but this one is
I asked a similar question a few days ago, but there's no harm in
In response to a question I asked a few days ago, I'm attempting to
A few days ago I asked a question about returning select fields from a

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.