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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:16:12+00:00 2026-05-22T12:16:12+00:00

I started this question with a load of background of the types in question;

  • 0

I started this question with a load of background of the types in question; the interfaces and rationale behind the architecture.

Then I realised – ‘This is SO – keep it simple and get to the point’.

So here goes.

I have a class like this:

public class AGenericType<T> : AGenericTypeBase
{
  T Value { get; set; }
}

.Net then, of course, allows me to do this:

AGenericType<AGenericType<int>> v;

However, in the context of AGenericType<T>‘s usage, it is a nonsense to do this in the same way that it’s a nonsense to do this:

Nullable<Nullable<double>> v;

What I want to be able to do is restrict this generic type so that it becomes impossible to create such an instance or even declare a reference to the type when it’s T is derived from AGenericTypeBase – preferably at compile-time.

Now an interesting thing here is that the Nullable<T> example I give here does indeed generate a compiler error. But I can’t see how Nullable<T> restricts the T to non-Nullable<T> types – since Nullable<T> is a struct, and the only generic constraint I can find (even in the IL, which often yields compiler secrets, like those with delegates) is where T:struct. So I’m thinking that one must be a compiler hack (EDIT: See @Daniel Hilgarth’s answer + comments below for a bit of an exploration of this). A compiler hack I can’t repeat of course!

For my own scenario, IL and C# don’t allow a negative-assert constraint like this:

public class AGenericType<T> : where !T:AGenericTypeBase

(note the ‘!’ in the constraint)

But what alternative can I use?

I’ve thought of two:

1) Runtime exception generated in the constructor of an AGenericType<T>:

public AGenericType(){
  if(typeof(AGenericBase).IsAssignableFrom(typeof(T)))
    throw new InvalidOperationException();
}

That doesn’t really reflect the nature of the error, though – because the problem is the generic parameter and therefore the whole type; not just that instance.

2) So, instead, the same runtime exception, but generated in a static initializer for AGenericType<T>:

static AGenericType(){
  if(typeof(AGenericBase).IsAssignableFrom(typeof(T)))
    throw new InvalidOperationException();
}

But then I’m faced with the problem that this exception will be wrapped inside a TypeInitializationException and could potentially cause confusion (in my experience developers that actually read a whole exception hierarchy are thin on the ground).

To me, this is a clear case for ‘negative assertion’ generic constraints in IL and C# – but since that’s unlikely to happen, what would you do?

  • 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-22T12:16:13+00:00Added an answer on May 22, 2026 at 12:16 pm

    In the end I have gone with an exception in the static initialiser of the generic type as it demonstrates more clearly that there is something wrong with the caller’s use of the type as opposed to a runtime argument passed to a constructor. The error is easily fixed by the caller, so I think its the best compromise.

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

Sidebar

Related Questions

I've just started learning ruby, so this question is simple. I created @subject in
I started to ask this question and then figured out the answer before submitting
I originally started this question in another thread, but that thread was sorta, kinda
I've started this question Named route with nested resources , and it lead me
Started with this question: OpenID. How do you logout OK. So OpenID does not
I know this question has kind-a started religious wars in past and there might
Forgive if this question is silly. I just started reading the SugarCRM developer documentation
This question most closely relates to the asp.net mvc3 framework. It started out as
Let me preface this question. I have just started using jquery, so please be
I've started to become a Good Citizen, as per this question here: Domain compatibility:

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.